Confusion with modules in Rust: main.rs and lib.rs in one project
Hi everyone! I'm a beginner in Rust and I can't figure out modules when both `main.rs` and `lib.rs` exist in the same project.\n\nI have this project:\n\n```\nmy_project/\n├── Cargo.toml\n├── src/\n│ ├── main.rs\n│ └── lib.rs\n```\n\nIn `lib.rs` I declared a module `utils`:\n\n```rust\n// lib.rs\nmod utils;\n\npub fn greet() {\n println!("Hello from lib!");\n}\n```\n\nAnd in `utils.rs` I have a function `add`:\n\n```rust\n// utils.rs\npub fn add(a: i32, b: i32) -> i32 {\n a + b\n}\n```\n\nIn `main.rs` I'm trying to call the `greet` function from `lib.rs` and the `add` function from the `utils` module:\n\n```rust\n// main.rs\nfn main() {\n greet(); // error\n println!("2 + 3 = {}", utils::add(2, 3)); // also error\n}\n```\n\nThe compiler complains: "cannot find function `greet` in this scope" and "u
No answers yet.
Log in to leave an answer
Blogs
Top Mistakes of Beginner C# Developers and How to Avoid Them