Created
July 1, 2016 16:48
-
-
Save matthewjberger/68ca053ded64538d59eee8633db7f8b7 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
extern crate rand; | |
use std::io; | |
use std::cmp::Ordering; | |
use rand::Rng; | |
fn main() { | |
let secret_number = rand::thread_rng().gen_range(1, 101); | |
let mut guesses = 0; | |
loop { | |
println!("Guess the number!"); | |
println!("Please input your guess."); | |
let mut guess = String::new(); | |
io::stdin() | |
.read_line(&mut guess) | |
.expect("Failed to read line"); | |
println!("You guessed: {}", guess); | |
let guess: u32 = match guess.trim().parse() { | |
Ok(num) => num, | |
Err(_) => continue, | |
}; | |
guesses += 1; | |
match guess.cmp(&secret_number) { | |
Ordering::Less => println!("Too small!"), | |
Ordering::Greater => println!("Too big!"), | |
Ordering::Equal => { | |
println!("You won in {} guesses!", guesses); | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment