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



















