Guessing Game Error

Hello Folks, am very new to programming. I just started with the rust documentary "The Book" few days ago. My dream is to be on the rust core team someday. I've come across a problem. Whenever I run the guessing game code, I get an error. Below is the code with the error message.

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);
}
error[E0061]: this function takes 1 argument but 2 arguments were supplied
 --> src\main.rs:7:44
  |
7 |     let secret_number = rand::thread_rng().gen_range(1, 101);
  |                                            ^^^^^^^^^ -  --- supplied 2 arguments
  |                                            expected 1 argument

error: aborting due to previous error

For more information about this error, try `rustc --explain E0061`.
error: could not compile `guessing_game`

To learn more, run the command again with --verbose.
PS C:\Users\futur\rust_projects\guessing_game> cargo run
   Compiling guessing_game v0.1.0 (C:\Users\futur\rust_projects\guessing_game)
error[E0061]: this function takes 1 argument but 2 arguments were supplied
  --> src\main.rs:40:44
   |
40 |     let secret_number = rand::thread_rng().gen_range(1, 101);
   |                                            ^^^^^^^^^ -  --- supplied 2 arguments
   |                                            |
   |                                            expected 1 argument

error: aborting due to previous error

For more information about this error, try `rustc --explain E0061`.
error: could not compile `guessing_game`

Please because am a newbie to programming, I kindly ask you to use simple English words to answer me. Explain it to me like you will do for a 5 year old. I use VS Code Editor. Thanks in advance for your help.

Do you have the same version of the rand crate as described on the book?

https://doc.rust-lang.org/stable/book/ch02-00-guessing-game-tutorial.html#updating-a-crate-to-get-a-new-version

Most recent version of the rand is 0.8.3 which has different API with the 0.6.0 used by the book.

1 Like

In particular, new versions of Rand have changed this to require .gen_range(1..101).

2 Likes

Thanks @Kestrer. You made my day beautiful. Awesome speedy reply. You're Awesome

Thanks @Hyeonu . Its resolved now. First changed it to the same version of rand in the book as you suggested and it worked wonderful. I then used the current version using .gen_range(1..101) as suggested by @Kestrer . Thanks again. Never thought the rust community is such vibrant.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.