Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
use std::io;
use std::rand;
fn main() {
let x:uint = rand::random();
let secret_number:uint = (x % 100u) + 1u;
println!("Guess the number!");
loop{
println!("Please input your guess.");
io::stdin().
let input = io::stdin().read_line().ok()
.expect("Failed to read line");
let input_num: Option<uint> = from_str(input.as_slice().trim());
let num = match input_num {
Some(num) => num,
None => {
println!("Please input a number!");
continue;
}
};
println!("You guessed: {}", num);
match cmp(num, secret_number) {
Less => println!("Too small!"),
Greater => println!("Too big!"),
Equal => {
println!("You win!");
return;
},
}
}
}
#[deriving(PartialEq,Show)]
enum Ordering {
Less,
Equal,
Greater,
}
fn cmp(a: uint, b: uint) -> Ordering {
if a < b { Less }
else if a > b { Greater }
else { Equal }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.