-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
21 additions
and
14 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,37 @@ | ||
extern crate rand; | ||
|
||
use std::io; | ||
use std::cmp::Ordering; | ||
use rand::Rng; | ||
use std::cmp::Ordering; | ||
use std::io; | ||
|
||
fn main() { | ||
println!("Guess the number!"); | ||
|
||
let secret_number = rand::thread_rng().gen_range(1, 101); | ||
println!("The secret number is: {}", secret_number); | ||
|
||
println!("Please input your guess."); | ||
loop { | ||
println!("Please input your guess."); | ||
|
||
let mut guess = String::new(); | ||
let mut guess = String::new(); | ||
|
||
io::stdin() | ||
.read_line(&mut guess) | ||
.expect("failed to read line"); | ||
io::stdin() | ||
.read_line(&mut guess) | ||
.expect("failed to read line"); | ||
|
||
let guess:i32 = guess.trim().parse().expect("Please type a number!"); | ||
let guess: i32 = match guess.trim().parse() { | ||
Ok(num) => num, | ||
Err(_) => continue, | ||
}; | ||
|
||
println!("You guessed: {}", guess); | ||
println!("You guessed: {}", guess); | ||
|
||
match guess.cmp(&secret_number) { | ||
Ordering::Less => println!("Too small!"), | ||
Ordering::Greater => println!("Too Big!"), | ||
Ordering::Equal => println!("You Win!") | ||
match guess.cmp(&secret_number) { | ||
Ordering::Less => println!("Too small!"), | ||
Ordering::Greater => println!("Too Big!"), | ||
Ordering::Equal => { | ||
println!("You Win!"); | ||
break; | ||
} | ||
} | ||
} | ||
} |