Created
January 20, 2015 09:10
-
-
Save changs/c239c652aed566f682f6 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; | |
enum Ordering { | |
Bigger, | |
Smaller, | |
Equal, | |
} | |
fn cmp(x: usize, y: usize) -> Ordering { | |
if x > y { | |
Ordering::Bigger | |
} else if x == y { | |
Ordering::Equal | |
} else { | |
Ordering::Smaller | |
} | |
} | |
fn main() { | |
println!("Type a number"); | |
let secret_number = (rand::random::<usize>() % 100) + 1; // secret_number: i32 | |
loop { | |
let input = io::stdin().read_line().ok().expect("Failed to read line"); | |
let input_num: Option<usize> = input.trim().parse(); | |
let num = match input_num { | |
Some(num) => num, | |
None => { | |
println!("Please input a number!"); | |
continue; | |
} | |
}; | |
let result = match cmp(secret_number, num) { | |
Ordering::Bigger => "Bigger", | |
Ordering::Smaller => "Smaller", | |
Ordering::Equal => { | |
println!("You win!"); | |
return; | |
} | |
}; | |
println!("{}", result); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment