From 9134be2f602427dbf772d71b55258226b0640805 Mon Sep 17 00:00:00 2001 From: willow <42willow@pm.me> Date: Wed, 11 Mar 2026 13:48:25 +1100 Subject: [PATCH 1/5] fix: remove suboptimal code * zig is faster * zig is safer * zig is easier * zig is cuter --- .gitignore | 13 +-- Cargo.lock | 141 ----------------------------- Cargo.toml | 6 -- projects/chp3/Cargo.toml | 6 -- projects/chp3/src/main.rs | 28 ------ projects/functions/Cargo.toml | 6 -- projects/functions/src/main.rs | 24 ----- projects/guessing_game/Cargo.lock | 133 --------------------------- projects/guessing_game/Cargo.toml | 7 -- projects/guessing_game/src/main.rs | 36 -------- projects/hello_cargo/Cargo.lock | 7 -- projects/hello_cargo/Cargo.toml | 6 -- projects/hello_cargo/src/main.rs | 3 - 13 files changed, 2 insertions(+), 414 deletions(-) delete mode 100644 Cargo.lock delete mode 100644 Cargo.toml delete mode 100644 projects/chp3/Cargo.toml delete mode 100644 projects/chp3/src/main.rs delete mode 100644 projects/functions/Cargo.toml delete mode 100644 projects/functions/src/main.rs delete mode 100644 projects/guessing_game/Cargo.lock delete mode 100644 projects/guessing_game/Cargo.toml delete mode 100644 projects/guessing_game/src/main.rs delete mode 100644 projects/hello_cargo/Cargo.lock delete mode 100644 projects/hello_cargo/Cargo.toml delete mode 100644 projects/hello_cargo/src/main.rs diff --git a/.gitignore b/.gitignore index 661c158..134999e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,2 @@ -# Ignore the build directory for all projects -**/target/ - -# Ignore the lock file (Optional: recommended for libraries, -# but usually kept for final applications/binaries) -# **/Cargo.lock - -# IDE specific files -.vscode/ -.idea/ -**/*.rs.bk \ No newline at end of file +# mac os +.DS_Store diff --git a/Cargo.lock b/Cargo.lock deleted file mode 100644 index 319f1b3..0000000 --- a/Cargo.lock +++ /dev/null @@ -1,141 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 4 - -[[package]] -name = "cfg-if" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" - -[[package]] -name = "chp3" -version = "0.1.0" - -[[package]] -name = "functions" -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.182" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6800badb6cb2082ffd7b6a67e6125bb39f18782f793520caee8cb8846be06112" - -[[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.106" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "quote" -version = "1.0.44" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21b2ebcf727b7760c461f091f9f0f539b77b8e87f2fd88131e7f1b433b3cece4" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "rand" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" -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.117" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "unicode-ident" -version = "1.0.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" - -[[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.40" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a789c6e490b576db9f7e6b6d661bcc9799f7c0ac8352f56ea20193b2681532e5" -dependencies = [ - "zerocopy-derive", -] - -[[package]] -name = "zerocopy-derive" -version = "0.8.40" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f65c489a7071a749c849713807783f70672b28094011623e200cb86dcb835953" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] diff --git a/Cargo.toml b/Cargo.toml deleted file mode 100644 index a7cddaf..0000000 --- a/Cargo.toml +++ /dev/null @@ -1,6 +0,0 @@ -[workspace] -members = [ - "projects/chp3", - "projects/functions", - "projects/guessing_game", -] diff --git a/projects/chp3/Cargo.toml b/projects/chp3/Cargo.toml deleted file mode 100644 index 11b095e..0000000 --- a/projects/chp3/Cargo.toml +++ /dev/null @@ -1,6 +0,0 @@ -[package] -name = "chp3" -version = "0.1.0" -edition = "2024" - -[dependencies] diff --git a/projects/chp3/src/main.rs b/projects/chp3/src/main.rs deleted file mode 100644 index 4f83d13..0000000 --- a/projects/chp3/src/main.rs +++ /dev/null @@ -1,28 +0,0 @@ -use std::io; - -fn main() { - let a = [1, 2, 3, 4, 5]; - - println!("Please enter an array index."); - - let mut index = String::new(); - - io::stdin() - .read_line(&mut index) - .expect("Failed to read line"); - - let index: usize = index - .trim() - .parse() - .expect("Index entered was not a number"); - - let element = a[index]; - - println!("The value of the element at index {index} is: {element}"); - let index: i32 = index.try_into().unwrap(); - another_function(index); -} - -fn another_function(x: i32) { - println!("The second function was run and the number is {x}") -} \ No newline at end of file diff --git a/projects/functions/Cargo.toml b/projects/functions/Cargo.toml deleted file mode 100644 index 9c392ab..0000000 --- a/projects/functions/Cargo.toml +++ /dev/null @@ -1,6 +0,0 @@ -[package] -name = "functions" -version = "0.1.0" -edition = "2024" - -[dependencies] diff --git a/projects/functions/src/main.rs b/projects/functions/src/main.rs deleted file mode 100644 index e0cf349..0000000 --- a/projects/functions/src/main.rs +++ /dev/null @@ -1,24 +0,0 @@ -use std::io; - -fn main(){ - //convert cel to far (cringe) - - let mut input = String::new(); - io::stdin() - .read_line(&mut input) - .expect("Failed to Read Line"); - - let input: f32 = match input.trim().parse::() { - Ok(num) => num, - Err(_) => { - println!("Not a number run again"); - return; - }, - }; - convert(input); -} - -fn convert(input: f32) { - let result = input * 1.8 + 32.0; - println!("{result}"); -} \ No newline at end of file diff --git a/projects/guessing_game/Cargo.lock b/projects/guessing_game/Cargo.lock deleted file mode 100644 index 9755c48..0000000 --- a/projects/guessing_game/Cargo.lock +++ /dev/null @@ -1,133 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 4 - -[[package]] -name = "cfg-if" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" - -[[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.182" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6800badb6cb2082ffd7b6a67e6125bb39f18782f793520caee8cb8846be06112" - -[[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.106" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "quote" -version = "1.0.44" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21b2ebcf727b7760c461f091f9f0f539b77b8e87f2fd88131e7f1b433b3cece4" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "rand" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" -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.117" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "unicode-ident" -version = "1.0.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" - -[[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.40" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a789c6e490b576db9f7e6b6d661bcc9799f7c0ac8352f56ea20193b2681532e5" -dependencies = [ - "zerocopy-derive", -] - -[[package]] -name = "zerocopy-derive" -version = "0.8.40" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f65c489a7071a749c849713807783f70672b28094011623e200cb86dcb835953" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] diff --git a/projects/guessing_game/Cargo.toml b/projects/guessing_game/Cargo.toml deleted file mode 100644 index 9f9c4ac..0000000 --- a/projects/guessing_game/Cargo.toml +++ /dev/null @@ -1,7 +0,0 @@ -[package] -name = "guessing_game" -version = "0.1.0" -edition = "2024" - -[dependencies] -rand = "0.8.5" diff --git a/projects/guessing_game/src/main.rs b/projects/guessing_game/src/main.rs deleted file mode 100644 index b6d8bf4..0000000 --- a/projects/guessing_game/src/main.rs +++ /dev/null @@ -1,36 +0,0 @@ -use std::cmp::Ordering; -use std::io; - -use rand::Rng; - -fn main() { - println!("Guess a number"); - - let secret_number = rand::thread_rng().gen_range(1..=100); - // println!("The Number was {secret_number}"); - - loop{ - println!("Please Input a number: "); - 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 Large"), - Ordering::Equal => { - println!("You Win!"); - break; - } - } - } -} diff --git a/projects/hello_cargo/Cargo.lock b/projects/hello_cargo/Cargo.lock deleted file mode 100644 index feebdd1..0000000 --- a/projects/hello_cargo/Cargo.lock +++ /dev/null @@ -1,7 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 4 - -[[package]] -name = "hello_cargo" -version = "0.1.0" diff --git a/projects/hello_cargo/Cargo.toml b/projects/hello_cargo/Cargo.toml deleted file mode 100644 index f43e5f8..0000000 --- a/projects/hello_cargo/Cargo.toml +++ /dev/null @@ -1,6 +0,0 @@ -[package] -name = "hello_cargo" -version = "0.1.0" -edition = "2024" - -[dependencies] diff --git a/projects/hello_cargo/src/main.rs b/projects/hello_cargo/src/main.rs deleted file mode 100644 index e7a11a9..0000000 --- a/projects/hello_cargo/src/main.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println!("Hello, world!"); -} From c7745440cf3e657ef02320db03858d138abca885 Mon Sep 17 00:00:00 2001 From: willow <42willow@pm.me> Date: Fri, 13 Mar 2026 17:45:59 +1100 Subject: [PATCH 2/5] feat: hello world --- hello-world.zig | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 hello-world.zig diff --git a/hello-world.zig b/hello-world.zig new file mode 100644 index 0000000..e035f92 --- /dev/null +++ b/hello-world.zig @@ -0,0 +1,5 @@ +const std = @import("std"); + +pub fn main() void { + std.debug.print("Hello, world!\n", .{}); +} From 8ce2708032df348bfc911246e7f433fb5a5eced7 Mon Sep 17 00:00:00 2001 From: willow <42willow@pm.me> Date: Fri, 13 Mar 2026 18:30:15 +1100 Subject: [PATCH 3/5] feat: celcius -> fahrenheit conversion --- functions.zig | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 functions.zig diff --git a/functions.zig b/functions.zig new file mode 100644 index 0000000..d5d6adc --- /dev/null +++ b/functions.zig @@ -0,0 +1,37 @@ +const std = @import("std"); + +pub fn main() !void { + const stdin = std.io.getStdIn().reader(); + const stdout = std.io.getStdOut(); + + try stdout.writeAll("enter temp: "); + + // fixed size, so its on the stack + var buf: [1024]u8 = undefined; + + // reads from stdin until delimeter encountered + // i.e. enter is pressed + // + // |line| is a payload capture that unwraps the returned optional, + // as the user could have entered no data (e.g. EOF) + if (try stdin.readUntilDelimiterOrEof(&buf, '\n')) |line| { + // {s} specifies line as a string + // std.debug.print("user entered: {s}\n", .{line}); + + if (std.fmt.parseFloat(f32, line)) |value| { + // {d} specifies decimal + // std.debug.print("float is: {d}\n", .{value}); + + convert(value); + } else |err| { + std.debug.print("please enter a valid float: {}\n", .{err}); + } + } else { + std.debug.print("please enter a valid float\n", .{}); + } +} + +fn convert(input: f32) void { + const result = input * 1.8 + 32.0; + std.debug.print("{d}°F\n", .{result}); +} From afb8df4f9c4a193cc489a816d5960b3a25626886 Mon Sep 17 00:00:00 2001 From: willow <42willow@pm.me> Date: Fri, 13 Mar 2026 19:03:48 +1100 Subject: [PATCH 4/5] feat: guess the number --- guess_the_number.zig | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 guess_the_number.zig diff --git a/guess_the_number.zig b/guess_the_number.zig new file mode 100644 index 0000000..2ba98c3 --- /dev/null +++ b/guess_the_number.zig @@ -0,0 +1,41 @@ +const std = @import("std"); + +pub fn main() !void { + const stdin = std.io.getStdIn().reader(); + const secret_number = try random(1, 100); + var buf: [1024]u8 = undefined; + + while (true) { + try std.debug.print("guess the number (1 - 100): ", .{}); + + if (try stdin.readUntilDelimiterOrEof(&buf, '\n')) |line| { + // parse base 10 integer + if (std.fmt.parseInt(u8, line, 10)) |guess| { + std.debug.print("you guessed {d}\n", .{guess}); + + switch (std.math.order(guess, secret_number)) { + .lt => std.debug.print("too small\n", .{}), + .gt => std.debug.print("too large\n", .{}), + .eq => { + std.debug.print("you win!\n", .{}); + break; + }, + } + } else |err| { + std.debug.print("please enter a valid integer: {}\n", .{err}); + } + } else { + std.debug.print("failed to read line", .{}); + } + } +} + +fn random(min: u8, max: u8) !u8 { + var seed: u64 = undefined; + try std.posix.getrandom(std.mem.asBytes(&seed)); + + var prng: std.Random.DefaultPrng = .init(seed); + const rand = prng.random(); + + return rand.intRangeAtMost(u8, min, max); +} From 485284e5f425238892c803bff88c8155479e9b60 Mon Sep 17 00:00:00 2001 From: willow <42willow@pm.me> Date: Fri, 13 Mar 2026 22:13:05 +1100 Subject: [PATCH 5/5] docs: update README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8f8fa4e..7c10c66 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # Hellow ( •̀ ω •́ )y -This is just me practicing rust \ No newline at end of file +his is just me practicing zig ^-^