[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