Created
January 13, 2015 03:03
-
-
Save chrisguitarguy/b9bf68b5b2b72a6f91ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io; | |
use std::rand; | |
use std::cmp::Ordering; | |
fn main() { | |
println!("Guess the number!"); | |
let secret_number: u32 = (rand::random::<u32>() % 100) + 1; | |
loop { | |
println!("Please enter your guess"); | |
let input = io::stdin().read_line() | |
.ok() | |
.expect("Failed to read line"); | |
let num = match input.trim().parse::<u32>() { | |
Some(n) => n, | |
None => { | |
println!("Please enter a number"); | |
continue | |
}, | |
}; | |
println!("You guessed {}", num); | |
match cmp(num, secret_number) { | |
Ordering::Greater => println!("Too big!"), | |
Ordering::Less => println!("Too small!"), | |
Ordering::Equal => { | |
println!("You win!"); | |
return; | |
} | |
} | |
} | |
} | |
fn cmp(a: u32, b: u32) -> Ordering { | |
if a < b { Ordering::Less } | |
else if a > b { Ordering::Greater } | |
else { Ordering::Equal } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment