Skip to content

Commit

Permalink
init add of ch02-00
Browse files Browse the repository at this point in the history
  • Loading branch information
retr0h committed Apr 22, 2024
1 parent f7bc9d4 commit d9ad2ad
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
1 change: 1 addition & 0 deletions ch02-00/guessing_game/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
75 changes: 75 additions & 0 deletions ch02-00/guessing_game/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions ch02-00/guessing_game/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "guessing_game"
version = "0.1.0"
edition = "2021"

[dependencies]
rand = "0.8.5"
35 changes: 35 additions & 0 deletions ch02-00/guessing_game/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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..=100);

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;
}
}
}
}

0 comments on commit d9ad2ad

Please sign in to comment.