Skip to content

Instantly share code, notes, and snippets.

@changs
Created January 20, 2015 09:10
Show Gist options
  • Save changs/c239c652aed566f682f6 to your computer and use it in GitHub Desktop.
Save changs/c239c652aed566f682f6 to your computer and use it in GitHub Desktop.
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