Solution: one-dimensional space-fight.
examples/number-guessing-game/number-guessing-game/Cargo.toml
[package] name = "number_guessing_game" version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] rand = "0.8.5"
examples/number-guessing-game/number-guessing-game/src/main.rs
use rand::Rng; use std::cmp::Ordering; use std::io; fn main() { println!("Number Guessing game"); let secret_number = rand::thread_rng().gen_range(1..=100); loop { println!("Input your guess: "); let mut guess = String::new(); io::stdin() .read_line(&mut guess) .expect("Failed to read line"); guess = guess.trim().to_owned(); if guess == "c" { println!("The secret number is {secret_number}"); continue; } if guess == "x" || guess == "q" { println!("Quitter!"); break; } println!("You guessed {guess}"); let guess: u32 = match guess.parse() { Ok(num) => num, Err(err) => { println!("Error: {err}"); continue; } }; match guess.cmp(&secret_number) { Ordering::Less => println!("Too small!"), Ordering::Greater => println!("Too big!"), Ordering::Equal => { println!("You win!"); break; } } } }