2.8 use Statements and Namespacing

2.8.1 Bringing Names into Scope

use std::io;

fn main() {
    let mut input = String::new();
    io::stdin().read_line(&mut input)
        .expect("Failed to read line");
    println!("You typed: {}", input);
}
  • use brings a path into scope, simplifying code.

2.8.2 Comparison with C

  • C uses #include to include headers.
#include <stdio.h>

int main() {
    char input[100];
    fgets(input, 100, stdin);
    printf("You typed: %s", input);
    return 0;
}
  • #include copies the entire file content; Rust's use is more precise.