I got the following error while compiling the guess game example.
error[E0432]: unresolved import `rand`
--> src/main.rs:2:5
|
2 | use rand::Rng;
| ^^^^ Maybe a missing `extern crate rand;`?
error[E0658]: access to extern crates through prelude is experimental (see issue #44660)
--> src/main.rs:7:25
|
7 | let secret_number = rand::thread_rng().gen_range(1, 101);
| ^^^^
warning: unused import: `rand::Rng`
--> src/main.rs:2:5
|
2 | use rand::Rng;
| ^^^^^^^^^
|
= note: #[warn(unused_imports)] on by default
error[E0599]: no method named `gen_range` found for type `rand::ThreadRng` in the current scope
--> src/main.rs:7:44
|
7 | let secret_number = rand::thread_rng().gen_range(1, 101);
| ^^^^^^^^^
|
= help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope, perhaps add a `use` for it:
|
1 | use rand::Rng;
|
error: aborting due to 3 previous errors
Some errors occurred: E0432, E0599, E0658.
For more information about an error, try `rustc --explain E0432`.
error: Could not compile `guess_game`.
Environment:
The error was resolved when I added extern crate rand;. Is the example out of date?
Your Rust compiler is out of date - the newest stable one is 1.31; try upgrading (rustup update).
I face the same issue when I build the guessing game. In the text, they mentioned it:
First, we add a line that lets Rust know we鈥檒l be using the rand crate as an external dependency.
but in the code, they forgot to add extern crate rand
@massih please see the comment above about rust versions; you need to be running rustc 1.31 or higher, and you need to have edition = "2018" in your Cargo.toml (which Cargo will add for you automatically on cargo new in that version and above)
@steveklabnik I am using rustc 1.31.1 but I created the project using intellij instead of cargo, and there was no edition = "2018" in the Cargo.toml. After adding the edition in the cargo file it works, probably I messed it up during the project creation in intellij. Thanks for quick response.
I struggled with this as well. Now I have it working.
First some references:
https://crates.io/crates/rand
https://rust-random.github.io/book/overview.html
And here is what I have that is working for me.
$ rustc --version && cargo --version
rustc 1.32.0 (9fda7c223 2019-01-16)
cargo 1.32.0 (8610973aa 2019-01-02)
$ cat Cargo.toml
[package]
name = "guessing_game"
version = "0.1.0"
authors = ["Greg"]
[dependencies]
rand = "0.6"
$ cat src/main.rs
extern crate rand;
use std::io;
use rand::Rng;
fn main() {
let secret_number = rand::thread_rng().gen_range(1, 101);
println!("Guess the number!");
let mut guess = String::new();
io::stdin().read_line(&mut guess)
.expect("Failed to read line");
println!("You guessed: {}", guess);
println!("The secret number is: {}", secret_number);
}
$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.05s
Running target/debug/guessing_game
Guess the number!
50
You guessed: 50
The secret number is: 19
Most helpful comment
@massih please see the comment above about rust versions; you need to be running rustc 1.31 or higher, and you need to have
edition = "2018"in yourCargo.toml(which Cargo will add for you automatically oncargo newin that version and above)