commit b924b7b5d2b433f2e9ebe1630b7bca063a563670 Author: Bartal Læarsson Date: Mon Sep 28 13:52:41 2026 +0100 complete section 1, guessing game diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..f60c6fb --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,137 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "cfg-if" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" + +[[package]] +name = "common-programming-concepts" +version = "0.1.0" + +[[package]] +name = "getrandom" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "guessing-game" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "libc" +version = "0.2.189" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2" + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e058c7de0b26af77780c769414d6257830bb240f3c38477dbc2c16e5f54d6d4c" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "syn" +version = "2.0.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d245f478577f809a851594d02313b640fb437e0bb33866753cff937863096954" + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "zerocopy" +version = "0.8.59" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6df92bf3d9227be3d53173901ddbffac2babc27ae50f397776ffd6dc33f800cb" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.59" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac4f328cf2f05d084e496c3e9c3f33ed0a183656a16e1fcec4d464d8373aec82" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..033c095 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[workspace] +members = ["common-programming-concepts","guessing-game"] +resolver = "2" + +[workspace.package] +edition = "2024" +version = "0.1.0" +license = "MIT" + diff --git a/common-programming-concepts/Cargo.toml b/common-programming-concepts/Cargo.toml new file mode 100644 index 0000000..99fe8a5 --- /dev/null +++ b/common-programming-concepts/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "common-programming-concepts" +edition.workspace = true +version.workspace = true +license.workspace = true + +[dependencies] diff --git a/common-programming-concepts/src/main.rs b/common-programming-concepts/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/common-programming-concepts/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/guessing-game/Cargo.toml b/guessing-game/Cargo.toml new file mode 100644 index 0000000..f4a68bb --- /dev/null +++ b/guessing-game/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "guessing-game" +version = "0.1.0" +edition = "2024" + +[dependencies] +rand = "0.8.5" diff --git a/guessing-game/src/main.rs b/guessing-game/src/main.rs new file mode 100644 index 0000000..1fbbf95 --- /dev/null +++ b/guessing-game/src/main.rs @@ -0,0 +1,61 @@ +// We import io from the standard library +// It is like stdin stdout stderr and all that +use std::cmp::Ordering; +use std::io; + +use rand::Rng; + +// Main entry point of application +fn main() { + // Use the println! macro to print to stdout + println!("Guess the number!"); + + let secret_number = rand::thread_rng().gen_range(1..=100); + + println!("Please input your guess."); + + // here we start the while ish loop + loop { + // uncomment the next line if you want to see the secret number as stdout + // println!("The secret number is {secret_number}"); + + // declare a mutable variable which is a growable empty string + let mut guess = String::new(); + + // from io in the std lib use stdin as input to app + // use read_line to read the input and stores it in a reference to the mutable variable guess + // Then expect is used as err handling, if something goes wrong, write to stderr and quit + // program with 1 + io::stdin() + .read_line(&mut guess) + .expect("failed to read line"); + + // In rust, when converting types, you usually use the same var name, instead of doing something + // like let guess_int and let guess_str. + // here we define guess as unsigned 32 bit int, we reuse the guess variable, then we do a trim + // of it, because user input can contain 4\n which would cause it to fail always, as it cannot + // be parsed to an int. The parse step knows that it should parse it to the defined u32 data type. + // The expect has the result of either error or ok, and terminates the program if not valid + // integer. + // So here the expect is exchanged with match and pattern matching with the two result + // values, "Ok" and "Err". This is for gracefull err handling and starting the while loop + // again, even if you enter something invalid. + // Parse returns Result as mentioned above, but also "num" which is the Ok number + let guess: u32 = match guess.trim().parse() { + Ok(num) => num, + Err(_) => continue, + }; + + // print the user input with extrapolated guess variable in {} brackets + 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; + } + } + } +}