首页 > 其他分享 >Rust: Guess Gaming

Rust: Guess Gaming

时间:2022-12-03 10:56:38浏览次数:43  
标签:Gaming Guess Ordering guess number secret let println Rust

 

use rand::Rng;
use std::{cmp::Ordering, io};
fn main() {
    println!("Hello, world!");
    let secret_number = rand::thread_rng().gen_range(1, 101);
    println!("secret_number: {}", secret_number);
    loop {
        println!("input your guess: ");

        let mut guess = String::new();
        io::stdin().read_line(&mut guess).expect("unable read");
        println!("guess is: {}", guess);

        // let guess: u32 = guess.trim().parse().expect("guess is not a number");
        let guess: u32 = match guess.trim().parse() {
            Ok(n) => n,
            Err(e) => {
                println!("Err: {}", e);
                continue;
            }
        };

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

 

标签:Gaming,Guess,Ordering,guess,number,secret,let,println,Rust
From: https://www.cnblogs.com/dissipate/p/16947134.html

相关文章