commit 668c7b10b5a6bcaafb9e5c84cdec187e197280ef Author: Ed Guloien Date: Sun Apr 26 17:29:18 2026 -0400 init simple project that reads a file, generates a random lowercase character and writes counts so far to a file 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..e7d6a82 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,133 @@ +# 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 = "char-counter" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[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 = "libc" +version = "0.2.186" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66" + +[[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.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca0ecfa931c29007047d1bc58e623ab12e5590e8c7cc53200d5202b69266d8a" +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.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eed437bf9d6692032087e337407a86f04cd8d6a16a37199ed57949d415bd68e9" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70e3cd084b1788766f53af483dd21f93881ff30d7320490ec3ef7526d203bad4" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..5e072e9 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "char-counter" +version = "0.1.0" +edition = "2024" + +[dependencies] +rand = "0.8" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..d1561a1 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,47 @@ +use std::fs; +use rand::rngs::OsRng; +use rand::Rng; + +fn random_lower_char(rng: &mut OsRng) -> u8 { + rng.gen_range(0..25) +} + +fn serialize_data(data: &[u32]) -> String { + println!("serializing..."); + let mut serialization = String::new(); + for (i, count) in data.iter().enumerate() { + let c = (i as u8 + 97) as char; + let s = format!("{} {}\n", c, count.to_string()); + serialization.push_str(&s) + } + // only stack object (ptr, len, capacity) is copied on the way out + serialization +} + +fn main() { + // considered using mutable static array for data + // but that requires unsafe, so we just use non-static memory + let mut data : [u32; 26] = [0; 26]; + + // deserialize file contents into data structure + if let Ok(contents) = fs::read_to_string("state.txt") { + println!("deserializing..."); + for line in contents.split('\n') { + if let Some((letter, val)) = line.split_once(' ') { + let index = (letter.chars().next().unwrap() as u32) - 97; // todo: handle failure + let count = val.parse::().unwrap(); // todo: handle failure + data[index as usize] = count; + } + } + } + + // generate random char and give to data structure + let mut rng = OsRng; + let index = random_lower_char(&mut rng) as usize; + data[index] += 1; + + // write data structure to file + let s_data = serialize_data(&data); + fs::write("state.txt", s_data).expect("Should be able to write to `state.txt`"); + println!("done") +} diff --git a/state.txt b/state.txt new file mode 100644 index 0000000..366c2a4 --- /dev/null +++ b/state.txt @@ -0,0 +1,26 @@ +a 0 +b 2 +c 1 +d 0 +e 0 +f 2 +g 2 +h 1 +i 1 +j 0 +k 1 +l 0 +m 4 +n 0 +o 2 +p 1 +q 1 +r 0 +s 1 +t 3 +u 0 +v 1 +w 4 +x 0 +y 0 +z 0