I'm able to get the guessing game example from The Book to run (via cargo run
) but not to compile (via rustc
). Here are the code and errors:
main.rs
use rand::Rng;
use std::cmp::Ordering;
use std::io::stdin;
fn main() {
println!("Guess the number!");
let secret_number: i32 = rand::thread_rng().gen_range(1..101);
println!("The secret number is {}.", secret_number);
println!("Please input your guess: ");
let mut guess: String = String::new();
stdin()
.read_line(&mut guess)
.expect("Failed to read line.");
let guess: i32 = guess.trim().parse().expect("Please type a number!");
println!("You guessed: {}", guess);
match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small."),
Ordering::Equal => println!("You win!"),
Ordering::Greater => println!("Too large."),
}
}
Cargo.toml
...
[dependencies]
rand = "0.8.3"
errors from rustc src/main.rs
error[E0432]: unresolved import `rand`
--> src/main.rs:1:5
|
1 | use rand::Rng;
| ^^^^
| |
| unresolved import
| help: a similar path exists: `io::sys::rand`
error[E0433]: failed to resolve: use of undeclared crate or module `rand`
--> src/main.rs:8:30
|
8 | let secret_number: i32 = rand::thread_rng().gen_range(1..101);
| ^^^^ use of undeclared crate or module `rand`
I'm still very new to Rust (this is the second chapter of the introductory book) and so haven't been able to troubleshoot those errors. Could someone help me?