Skip to content

Guessing Game rand example doesn't compile #1663

@zvory

Description

@zvory

Versions

Book:

rustc: rustc 1.31.0 (abe02cefd 2018-12-04)

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

What is the issue:

When going through the Guessing Game tutorial:

Listing 2-3 contains some code that should use rand:

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);
}

This code does not compile, with the following errors:

azvorygi@bit-shifter:~/Code/rst$ cargo run
   Compiling rst v0.1.0 (/users/azvorygi/Code/rst)                                                                                                                                            
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 `rst`.                                                                                                                                                               

This is of course after adding the following to my Cargo.toml:

[dependencies]
rand = "0.3.14"

Here's a github repo with my repo when it is breaking: https://github.com/zvory/guessing-game-breaking

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions