Created
March 22, 2019 10:42
-
-
Save katrinafyi/11157ceca85bc531530148a6200f75f7 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::io::Write; | |
use rand::Rng; | |
use std::cmp::Ordering; | |
use std::num::ParseIntError; | |
fn parse_str(s: &str) -> Result<u32, ParseIntError> { | |
s.trim().parse::<u32>()? | |
} | |
fn main() { | |
println!("Hello, world!"); | |
let secret: u32 = rand::thread_rng().gen_range(1, 101); | |
loop { | |
print!("Enter a guess: "); | |
io::stdout().flush().expect("Error flushing stdout."); | |
let mut guess = String::new(); | |
io::stdin().read_line(&mut guess).expect("Error reading line."); | |
let guess = parse_str(&guess).ok(); | |
let a = 2.0/3.0; | |
println!("{}", a); | |
let result = match (guess.cmp(&secret)) { | |
Ordering::Less => "Too small!", | |
Ordering::Equal => { | |
println!("You win!"); | |
break; | |
}, | |
Ordering::Greater => "Too big!" | |
}; | |
println!("{}", result); | |
println!("You guessed {}.", guess); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment