Modules in Rust
A module tree inside one file: mod declares, pub opens, paths reach in.
mod tours {
pub mod rust {
pub fn count() -> usize {
11
}
}
pub fn label() -> String {
format!("{} tours", rust::count())
}
}
fn main() {
println!("{}", tours::label());
println!("{}", tours::rust::count());
println!("{}", crate::tours::rust::count());
}
How it works
- Items are private to their module until marked
pub. - A nested module needs
pubat every level of the path. crate::starts a path from the root.
Keywords and builtins used here
Stringcountcratefnlabelmainmodpubusize
The run, in numbers
- Lines
- 17
- Characters to type
- 256
- Tokens
- 91
- Three-star pace
- 100 tpm
At the three-star pace of 100 tokens a minute, this run takes about 55 seconds.
Step 1 of 3 in Modules & visibility, step 1 of 12 in Modules, tests & macros.