typestar

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

  1. Items are private to their module until marked pub.
  2. A nested module needs pub at every level of the path.
  3. crate:: starts a path from the root.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 3 in Modules & visibility, step 1 of 12 in Modules, tests & macros.

Next →