Confusion with modules in Rust when having main.rs and lib.rs
Hello! I'm new to Rust and trying to understand the module system. I have a project that I created with `cargo new my_project`. Inside, there are two files: `main.rs` and `lib.rs`. I thought I could just declare a module in one of them and use it in the other, but nothing works.
Here's my structure:
```
my_project/
├── Cargo.toml
├── src/
│ ├── main.rs
│ └── lib.rs
```
I want to define a function in `lib.rs` and call it from `main.rs`. I tried writing in `lib.rs`:
```rust
mod utils {
pub fn greet(name: &str) -> String {
format!("Hello, {}!", name)
}
}
```
And in `main.rs`:
```rust
mod utils;
use utils::greet;
fn main() {
println!("{}", greet("Alice"));
}
```
But the compiler gives an error: `error[E0432]: unresolved impo
No answers yet.
Log in to leave an answer
Blogs