Book: Faulty example code in guessing game

Created on 3 Mar 2018  路  1Comment  路  Source: rust-lang/book

The following code example from the page/section https://doc.rust-lang.org/book/second-edition/ch02-00-guessing-game-tutorial.html#comparing-the-guess-to-the-secret-number is faulty.

````
extern crate rand;

use std::io;
use std::cmp::Ordering;
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);

match guess.cmp(&secret_number) {
    Ordering::Less => println!("Too small!"),
    Ordering::Greater => println!("Too big!"),
    Ordering::Equal => println!("You win!"),
}

}
````

It produces the following output, based upon the tutorial being followed:

image

I would normally just fix the problem, however my language experience with Rust is rather primitive.

Most helpful comment

Keep reading!

However, the code in Listing 2-4 won鈥檛 compile yet. Let鈥檚 try it:

>All comments

Keep reading!

However, the code in Listing 2-4 won鈥檛 compile yet. Let鈥檚 try it:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ctsa picture ctsa  路  3Comments

istarus picture istarus  路  4Comments

devimc picture devimc  路  4Comments

serialhex picture serialhex  路  4Comments

binarycrusader picture binarycrusader  路  4Comments