Skip to content

Commit 0e99f53

Browse files
committed
rust book wip
1 parent 697ad81 commit 0e99f53

File tree

4 files changed

+168
-0
lines changed

4 files changed

+168
-0
lines changed

rust/guessing_game/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

rust/guessing_game/Cargo.lock

Lines changed: 133 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/guessing_game/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "guessing_game"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
rand = "0.8.5"

rust/guessing_game/src/main.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use rand::Rng;
2+
use std::{cmp::Ordering, io};
3+
4+
fn main() {
5+
println!("Guess the number!");
6+
let secret_number = rand::thread_rng().gen_range(1..=100);
7+
loop {
8+
println!("Please input your guess.");
9+
let mut guess = String::new();
10+
io::stdin()
11+
.read_line(&mut guess)
12+
.expect("Failed to read line");
13+
let guess: u32 = match guess.trim().parse() {
14+
Ok(num) => num,
15+
Err(_) => continue,
16+
};
17+
println!("You guessed: {guess}");
18+
match guess.cmp(&secret_number) {
19+
Ordering::Less => println!("Too small!"),
20+
Ordering::Greater => println!("Too big!"),
21+
Ordering::Equal => {
22+
println!("You win!");
23+
break;
24+
}
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)