typestar

use, as and re-exports in Rust

Bringing names into scope, renaming clashes, and re-exporting an API.

mod store {
    pub mod inner {
        pub struct Row(pub u32);
    }
    pub use inner::Row;

    pub fn make() -> Row {
        Row(7)
    }
}

mod client {
    use super::store::{self, Row};

    pub fn read() -> u32 {
        let row: Row = store::make();
        row.0
    }
}

use std::collections::HashMap as Table;

fn main() {
    println!("{}", client::read());
    let mut t: Table<&str, u32> = Table::new();
    t.insert("rows", client::read());
    println!("{:?}", t);
}

How it works

  1. use shortens a path; as renames it.
  2. super:: refers to the parent module.
  3. pub use re-exports, flattening an internal tree.

Keywords and builtins used here

The run, in numbers

Lines
28
Characters to type
405
Tokens
145
Three-star pace
100 tpm

At the three-star pace of 100 tokens a minute, this run takes about 87 seconds.

Type this snippet

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

← Previous Next →