mod.rs 836 B

123456789101112131415161718192021222324252627282930313233343536
  1. //[dependencies]
  2. //rand = "0.7.3"
  3. use std::io;
  4. use rand::Rng;
  5. use std::cmp::Ordering;
  6. pub fn main() {
  7. println!("Guess the number!");
  8. let secret_number = rand::thread_rng().gen_range(1, 101);
  9. loop {
  10. println!("Please input your guess.");
  11. let mut guess = String::new();
  12. io::stdin().read_line(&mut guess)
  13. .expect("Failed to read line");
  14. let guess: u32 = match guess.trim().parse() {
  15. Ok(num) => num,
  16. Err(_) => continue,
  17. };
  18. println!("You guessed: {}", guess);
  19. match guess.cmp(&secret_number) {
  20. Ordering::Less => println!("Too small!"),
  21. Ordering::Greater => println!("Too big!"),
  22. Ordering::Equal => {
  23. println!("You win!");
  24. break;
  25. }
  26. }
  27. }
  28. }