When/why do Rust traits need to be used/Imported, or why does this question not make sense?
I'm an experienced programmer, but I'm new to Rust.
I'm working my way through The Rust Programming Language -- in chapter 2 there's the following statment.
First, we add a
useline:use rand::Rng. TheRngtrait defines methods that random number generators implement, and this trait must be in scope for us to use those methods. Chapter 10 will cover traits in detail.
Emphasis mine.
It's not clear to me why this program
use std::io;
use rand::Rng;
fn main() {
println!("Guess the number!");
let secret_number = rand::thread_rng().gen_range(1, 101);
println!("The secret number is: {}", secret_number);
println!("Please input your guess.");
let mut guess = String::new();
io::stdin()
.read_line(&mut guess)
.expect("Failed to read line");
println!("You guessed: {}", guess);
}
needs to use rand::Rng. When we use/imported the std::io symbol, we got access to an io symbol that we could call associated functions on.
io::stdin
In my naive (and incorrect) view, using use rand::Rng would give us an Rng symbol. Instead, we got a rand symbol that we could call a thread_rng method on.
let secret_number = rand::thread_rng().gen_range(1, 101);
My naive view would be that in order to call thread_rng, we'd need to do something like
use rand;
// or
use rand::rand
What am I, as the programmer, doing when I put use rand::Rng in my program, and why is this different from use std::io?