diff options
| author | hybrid <hybrid@hybridlabs.cc> | 2026-09-29 13:26:31 +0300 |
|---|---|---|
| committer | hybrid <hybrid@hybridlabs.cc> | 2026-09-29 13:26:31 +0300 |
| commit | d13f336198c40abacff6996f118040ae73dfbb30 (patch) | |
| tree | 88d96ff022c2d2fec969da3056d49477bda1f59a /src | |
| download | rust-book-d13f336198c40abacff6996f118040ae73dfbb30.tar.gz rust-book-d13f336198c40abacff6996f118040ae73dfbb30.tar.bz2 rust-book-d13f336198c40abacff6996f118040ae73dfbb30.zip | |
add: hello_world, hello_cargo and guessing_game projects
Diffstat (limited to 'src')
| -rw-r--r-- | src/guessing_game/Cargo.toml | 7 | ||||
| -rw-r--r-- | src/guessing_game/src/main.rs | 38 | ||||
| -rw-r--r-- | src/hello_cargo/Cargo.toml | 6 | ||||
| -rw-r--r-- | src/hello_cargo/src/main.rs | 3 | ||||
| -rw-r--r-- | src/hello_world/Cargo.toml | 6 | ||||
| -rw-r--r-- | src/hello_world/src/main.rs | 3 |
6 files changed, 63 insertions, 0 deletions
diff --git a/src/guessing_game/Cargo.toml b/src/guessing_game/Cargo.toml new file mode 100644 index 0000000..15924f9 --- /dev/null +++ b/src/guessing_game/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "guessing_game" +version = "0.1.0" +edition = "2024" + +[dependencies] +rand = "0.10.1"
\ No newline at end of file diff --git a/src/guessing_game/src/main.rs b/src/guessing_game/src/main.rs new file mode 100644 index 0000000..029eb36 --- /dev/null +++ b/src/guessing_game/src/main.rs @@ -0,0 +1,38 @@ +use std::io; +use std::cmp::Ordering; + +use rand::prelude::*; + +fn main() { + println!("Guess the number!"); + + let secret_number = rand::rng().random_range(1..=100); + + //println!("The secret number is: {secret_number}"); + + 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; + } + } + } +} diff --git a/src/hello_cargo/Cargo.toml b/src/hello_cargo/Cargo.toml new file mode 100644 index 0000000..f43e5f8 --- /dev/null +++ b/src/hello_cargo/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello_cargo" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/src/hello_cargo/src/main.rs b/src/hello_cargo/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/src/hello_cargo/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/src/hello_world/Cargo.toml b/src/hello_world/Cargo.toml new file mode 100644 index 0000000..4f0a461 --- /dev/null +++ b/src/hello_world/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello_world" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/src/hello_world/src/main.rs b/src/hello_world/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/src/hello_world/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} |
