liveBook · Manning

Namespace

As explained in Refactoring to Rust

A namespace is a named scope that contains identifiers and distinguishes them from identifiers in other scopes.

A namespace is a named scope that contains identifiers and distinguishes them from identifiers in other scopes. Rust modules create namespaces, so moving functions into input and output modules changes how main resolves their names. Public visibility and use statements restore access without changing the program’s behavior. section 5.1

Use paths to locate items across Rust namespaces:

  • Use a relative path such as hello when referring to an item from the current namespace. section 5.2.1
  • Use an absolute path such as std::io::stdin from a crate root or crate::treats::Treat within the current crate. section 5.2.1
  • Use super to refer to the namespace above the current one. section 5.2.1
  • Mark modules and items pub when code outside their declaring namespace must access them. section 5.1 section 5.2.1
  • Use use statements to bring selected names into the current namespace and shorten repeated paths. section 5.1 section 5.2.1

Refactoring a greeting program

Place get_name in an input module and hello and goodbye in an output module. Calling these functions directly from main produces not-found errors because they are no longer in main’s namespace. Add use input::get_name and use output::{goodbye, hello}, then mark the functions pub; the program runs as before. section 5.1

Listing 5.2 User greeting program with modules added
fn main() {
  let name = get_name();

  hello(&name);
  goodbye(&name);
}

mod input {
  use std::io::stdin;

  fn get_name() -> String {
    let mut name = String::new();

    println!("Please enter your name");
    std::io::stdin().read_line(&mut name).unwrap();

    name
  }
}

mod output {
  fn goodbye(name: &str) {
    println!("Goodbye, {}", name);
  }

  fn hello(name: &str) {
    println!("Hello, {}", name);
  }
}

Inspect the greeting program after modules separate input and output names, before imports and public visibility resolve its compiler errors. section 5.1

How Refactoring to Rust covers it

How deep it goes
Explains module namespaces, visibility, imports, and relative or absolute paths through progressively refactored examples.
Examples
Demonstrates greeting modules and a libsnack crate using imports, pub items, crate paths, and super paths.
What it assumes
Assumes basic Rust functions and crates while teaching module boundaries, imports, visibility, and paths incrementally.
What you can do after
Diagnose name-resolution errors and organize functions or types with Rust modules, visibility, imports, and paths.
How it connects
Connects namespaces with modules, crates, paths, visibility, and use statements.

Also explained in

Compare how 20 books explain namespace →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Refactoring to Rust. The section links point to the parts of the book it draws on.