Adding hint logic to Guessing Game [Resolved]

Hey y'all,

I'm new to the forum and also new to Rust. Currently reading through the Rust Programming Language book.

I finished building the Guessing Game and I wanted to improve upon it by adding a way for someone to receive a hint. I've experimented with placing if statements at various points in my code but nothing has worked so far.

Basically what I was trying to do would have looked something like this but the if statement is ignored for some reason. The code block below would be happening inside a loop.

if guess == "hint" {
    println!("The number is somewhere between {} and {}.", {secret_number - 10}, {secret_number + 10})
} else {
    let guess: u32 = match guess.trim().parse() {
        Ok(num) => num,
        Err(_) => {
            println!("Please input a number!");
            continue;
        }
    };

    println!("You guessed: {}", guess);

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

There doesn't seem to be an opportunity to inject any conditional logic before the shadowing of the guess variable takes place. I'll post the default working code below.

use rand::Rng;
use std::cmp::Ordering;
use std::io;

fn main() {
    println!("Guess the number!");

    let secret_number = rand::thread_rng().gen_range(1..101);

    loop {
        println!("Please input your guess.");

        let mut guess = String::new();

        io::stdin()
            .read_line(&mut guess)
            .expect("Failed to read line.");

        let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => {
                println!("Please input a number!");
                continue;
            }
        };

        println!("You guessed: {}", guess);

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

You forgot to call .trim() before checking if the input was "guess".
Here's a straightforward way to set it up in the loop:

let guess: u32 = match guess.trim() {
    "guess" => {
        println!("The number is somewhere between {} and {}.", {secret_number - 10}, {secret_number + 10});
        continue;
    }

    guess => match guess.parse() {
        Ok(num) => num,
        Err(_) => {
            println!("Please input a number!");
            continue;
        }
    }
};

I tried to implement the code block you provided but I got a strange error.

thread 'rustc' panicked at 'found unstable fingerprints for predicates_of(core[7cda]::ops::deref::Deref): GenericPredicates { parent: None, predicates: [(Binder(TraitPredicate(<Self as std::ops::Deref>)), /Users/vayu/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/deref.rs:64:1: 64:16 (#0))] }', /rustc/88f19c6dab716c6281af7602e30f413e809c5974/compiler/rustc_query_system/src/query/plumbing.rs:593:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.52.0 (88f19c6da 2021-05-03) running on x86_64-apple-darwin

note: compiler flags: -C embed-bitcode=no -C debuginfo=2 -C incremental --crate-type bin

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [predicates_of] computing predicates of `std::ops::Deref`
#1 [typeck] type-checking `main`
end of query stack
error: could not compile `guessing_game`

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

Is this really a bug that I should be reporting? Or is this an error because of some issue with the logic?

Somehow I managed to fix it by deleting the project and making it.

That's a bug, please report it if you can't find any existing reports. Looks like a caching bug.

3 Likes

Thanks for your help guys!

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.