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