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); }
usebrings a path into scope, simplifying code.
2.8.2 Comparison with C
- C uses
#includeto include headers.
#include <stdio.h>
int main() {
char input[100];
fgets(input, 100, stdin);
printf("You typed: %s", input);
return 0;
}
#includecopies the entire file content; Rust'suseis more precise.