regen guessing game
This commit is contained in:
parent
a2527b6fd7
commit
fc77000e3a
1
rustbook/guessing_game/.gitignore
vendored
1
rustbook/guessing_game/.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
target/
|
target/
|
||||||
|
Cargo.lock
|
||||||
|
20
rustbook/guessing_game/Cargo.lock
generated
20
rustbook/guessing_game/Cargo.lock
generated
@ -1,20 +0,0 @@
|
|||||||
[root]
|
|
||||||
name = "guessing_game"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "libc"
|
|
||||||
version = "0.2.15"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "rand"
|
|
||||||
version = "0.3.14"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
dependencies = [
|
|
||||||
"libc 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
||||||
]
|
|
||||||
|
|
@ -4,4 +4,3 @@ version = "0.1.0"
|
|||||||
authors = ["Dan Buch <daniel.buch@gmail.com>"]
|
authors = ["Dan Buch <daniel.buch@gmail.com>"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rand = "0.3.0"
|
|
||||||
|
@ -1,36 +1,3 @@
|
|||||||
extern crate rand;
|
|
||||||
|
|
||||||
use std::io;
|
|
||||||
use std::cmp::Ordering;
|
|
||||||
use rand::Rng;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Guess the number!");
|
println!("Hello, world!");
|
||||||
|
|
||||||
let secret_number = rand::thread_rng().gen_range(1, 101);
|
|
||||||
|
|
||||||
loop {
|
|
||||||
println!("Please input your guess.");
|
|
||||||
|
|
||||||
let mut guess = String::new();
|
|
||||||
|
|
||||||
io::stdin().read_line(&mut guess)
|
|
||||||
.expect("Failed to read line");
|
|
||||||
|
|
||||||
let guess: u32 = match guess.trim().parse() {
|
|
||||||
Ok(num) => num,
|
|
||||||
Err(_) => continue,
|
|
||||||
};
|
|
||||||
|
|
||||||
println!("You guessed: {}", guess);
|
|
||||||
|
|
||||||
match guess.cmp(&secret_number) {
|
|
||||||
Ordering::Less => println!("Too small!"),
|
|
||||||
Ordering::Greater => println!("Too big!"),
|
|
||||||
Ordering::Equal => {
|
|
||||||
println!("You win!");
|
|
||||||
break;
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user