How to import a module from another file in the same Rust project?
I just started learning Rust and I'm trying to understand the module system. I have a simple project created via `cargo new my_project`. It has the standard `src/main.rs` where I put my main logic. But the code is getting too large, and I want to move some functions into separate files.
I created a file `src/math.rs` with a simple function:
```rust
// src/math.rs
fn add(x: i32, y: i32) -> i32 {
x + y
}
```
Now I want to use this function in `src/main.rs`. I tried writing `mod math;` and then calling `math::add(2, 3)`, but the compiler gives an error: `error[E0603]: function `add` is private`. I also tried different variations with `use`, but nothing works.
I read the documentation, but it seems complicated. Please explain how to properly organize the code.
No answers yet.
Log in to leave an answer