Created
August 10, 2014 19:42
-
-
Save dahankzter/b89a2991e9bdcce8dd7e 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; | |
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