[Solved] Error while trying to compile the Guessing Game of the book


#1

This might be a problem due to unmatching software/documentation, however I wouldn’t expect such a simple example to no longer compile due to language or standard library evolution, so I guess it comes from somewhere else.

Here are the relevant information:

psychoslave@tony:~/Projektaro/rust/divenludo$ rustc --version && cargo --version
rustc 1.30.1 (1433507eb 2018-11-07)
cargo 1.30.0 (a1a4ad372 2018-11-02)
psychoslave@tony:~/Projektaro/rust/divenludo$ cat src/main.rs 
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);
}
psychoslave@tony:~/Projektaro/rust/divenludo$ cargo run
   Compiling divenludo v0.1.0 (/home/psychoslave/Projektaro/rust/divenludo)                                                 
error[E0432]: unresolved import `rand`                                                                                      
 --> src/main.rs:2:5                                                                                                        
  |                                                                                                                         
2 | use rand::Rng;                                                                                                          
  |     ^^^^ Maybe a missing `extern crate rand;`?                                                                          
                                                                                                                            
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 2 previous errors                                                                                    
                                                                                                                            
Some errors occurred: E0432, E0599.                                                                                         
For more information about an error, try `rustc --explain E0432`.                                                           
error: Could not compile `divenludo`.                                                                                       

To learn more, run the command again with --verbose.

I’ll investigate the error messages by myself, I guess I should be able to deal with the problem through available documentation, although any help is still welcome.

But I’m more concerned on the stability of the language and its main API, or the asynchronous release of software and documentation that makes such a case of compile error possible while following the official beginner tutorial.

Thank you in advance for your feedback.


#2

You left out this line at the top of the example in the book:

extern crate rand;

#3

Thank you @mmstick for your answer and quickness in reply.

All appologies for the noise request, as it was a rather trivial error from my part.

Cheers


#4

Actually the example does not show that first line (extern crate rand;)


#5

This is likely because the example has been updated to Rust 2018 - newly created projects (and projects that upgrade the edition in their Cargo.toml) on Rust 1.31 and higher no longer require extern crate.


#6

Interesting. I’m just following The Book, and had the same issue from this thread, and adding the extern fixed things.

I have the latest versions of rustup, rustc, cargo, etc. since I just installed them:

rustup --version
rustup 1.16.0 (beab5ac2b 2018-12-06)

rustc --version
rustc 1.31.1 (b6c32da9b 2018-12-18)

cargo --version
cargo 1.31.0 (339d9f9c8 2018-11-16)

When using cargo new (following along with the book), my Cargo.toml does not include an edition section, despite the fact that the cargo docs state:

The edition key affects which edition your package is compiled with. Cargo will always generate packages via cargo new with the edition key set to the latest edition.


#7

That sounds like a bug. Could you provide steps to reproduce in a Cargo issue please?


#9

I’m in the same boat. I followed the steps for windows from:
https://doc.rust-lang.org/book/ch01-00-getting-started.html

And ran into the same issue in chapter 2.


#10

Adding 2018 edition to the Cargo.toml fixed the issue for me.

[package]
    edition = "2018