Modules & CratesIntermediate
1Question 1 of 1
rustHow does the module system (mod, use, crates) structure Rust applications?
Explanation & Concept
Rust organizes code hierarchically: • Package: A Cargo project containing a `Cargo.toml` and one or more crates. • Crate: A compilation unit (binary `main.rs` or library `lib.rs`). • Module: Declared via `mod`, organizing privacy boundaries (`pub` vs private by default).
Code Example
rust
// In src/network/mod.rs
pub mod client {
pub fn connect() {
println!("Connected to network.");
}
}
// In src/main.rs
mod network;
use network::client;
fn main() {
client::connect();
}How It Works & Key Takeaways
Items in Rust are private by default, enforcing strict encapsulation and explicit public interfaces.
Mock Test · 1 Questions · Optional
Ready for Iterators?
Take a quick test to evaluate yourself, or move straight to the next module.
Intermediate1 questions