diff --git a/rust_session/src/array.rs b/rust_session/src/array.rs index 938c12f5..cfc2cca6 100644 --- a/rust_session/src/array.rs +++ b/rust_session/src/array.rs @@ -1,3 +1,4 @@ +#[allow(dead_code)] pub fn test_array() { let arr: [u8; 3] = [10, 7, 10]; println!("array here: {:?}", arr); diff --git a/rust_session/src/main.rs b/rust_session/src/main.rs index c11cc20e..043c24c4 100644 --- a/rust_session/src/main.rs +++ b/rust_session/src/main.rs @@ -1,8 +1,13 @@ -mod sub; -mod sum; -mod ownership; -mod array; -mod mut_ex; +use crate::todo::Todo; + +// mod sub; +// mod sum; +// mod ownership; +// mod array; +// mod mut_ex; +mod voters; +mod todo; + fn main() { @@ -13,5 +18,24 @@ fn main() { // array::test_array(); // ownership::test_move(); // ownership::call_greet(); - mut_ex::test_mut(); + // mut_ex::test_mut(); + voters::check_voter_eligibility(20); + + let todo1 = Todo::create_todo( + 1, + "Buy milk".to_string(), + "Get 2 liters of whole milk".to_string(), + todo::Status::Pending, + ); + println!("{:?}", todo1); + + let todo2 = Todo::add_todo( + 1, // current highest ID + "Walk the dog".to_string(), + "Around the block twice".to_string(), + todo::Status::Ongoing, + ); + println!("{:?}", todo2); } + + diff --git a/rust_session/src/ownership.rs b/rust_session/src/ownership.rs index 0d4b7975..002b76e2 100644 --- a/rust_session/src/ownership.rs +++ b/rust_session/src/ownership.rs @@ -1,9 +1,11 @@ +#[allow(dead_code)] pub fn test_ownership() { let name = String::from("Alice"); // `name` is the owner of this String // name.push('r'); println!("name is: {}", name); } +#[allow(dead_code)] pub fn call_name() { let name: &str = "Yusrah"; // name. @@ -11,7 +13,7 @@ pub fn call_name() { println!("name is: {}", name); } - +#[allow(dead_code)] pub fn test_move() { let a = String::from("Anagkazo"); println!("{}", a.clone()); // ✅ compiles: another address has been created in memory @@ -21,10 +23,12 @@ pub fn test_move() { println!("{}", b); // ✅ `b` is the owner now } +#[allow(dead_code)] pub fn greet(s: String) { // `s` takes ownership println!("Hello, {}!", s); } // `s` is dropped here +#[allow(dead_code)] pub fn call_greet() { let name = String::from("Bob"); greet(name); // ownership moves into `greet` diff --git a/rust_session/src/sub.rs b/rust_session/src/sub.rs index ea9141f9..a23d7d71 100644 --- a/rust_session/src/sub.rs +++ b/rust_session/src/sub.rs @@ -1,3 +1,4 @@ +#[allow(dead_code)] pub fn sub(x: u8, y: u8) -> u8 { let result = x - y; println!("result is: {result}"); diff --git a/rust_session/src/sum.rs b/rust_session/src/sum.rs index 78e40419..e3da4447 100644 --- a/rust_session/src/sum.rs +++ b/rust_session/src/sum.rs @@ -1,3 +1,4 @@ +#[allow(dead_code)] fn sum(x: u8, y: u8) -> u8 { x + y // return x + y; diff --git a/rust_session/src/todo.rs b/rust_session/src/todo.rs new file mode 100644 index 00000000..548633a5 --- /dev/null +++ b/rust_session/src/todo.rs @@ -0,0 +1,42 @@ +#[derive(Debug, PartialEq)] +#[allow(dead_code)] +pub enum Status { + Pending, + Ongoing, + Completed, + Cancel, +} + +#[derive(Debug)] +#[allow(dead_code)] +pub struct Todo { + id: u8, + title: String, + description: String, + status: Status, +} + +impl Todo { + pub fn create_todo(id: u8, title: String, description: String, status: Status) -> Todo { + let todo: Todo = Todo { + id, + title, + description, + status + }; + println!("You have created a new todo"); + return todo; + } + + pub fn add_todo(current_id: u8, title: String, description: String, status: Status) -> Todo { + let new_id: u8 = current_id + 1; + let todo: Todo = Todo { + id: new_id, + title, + description, + status + }; + println!("You have added a new todo with ID: {}", new_id); + return todo; + } +} diff --git a/rust_session/src/voters.rs b/rust_session/src/voters.rs new file mode 100644 index 00000000..4c94168b --- /dev/null +++ b/rust_session/src/voters.rs @@ -0,0 +1,65 @@ +#[derive(Debug)] // It can be called a macro, attribute or annotation +pub enum AgeGroup { + Juvenile, + Silver, + Golden, + Platinum, + Centurion, +} + +impl AgeGroup { + pub fn from_age(age: u32) -> AgeGroup { + match age { + 0..=17 => { + println!("Go back home!"); + AgeGroup::Juvenile + } + 18..=25 => { + println!("You're above the age of 18"); + AgeGroup::Silver + } + 26..=50 => { + println!("You're above the age of 25."); + AgeGroup::Golden + } + 51..=75 => { + println!("You're above the age of 50."); + AgeGroup::Platinum + } + _ => { + println!("You're above the age of 75 now."); + AgeGroup::Centurion + } + + } + } +} + + +#[derive(Debug)] +pub enum VoterEligibility { + Eligible, + NotEligible, +} + +#[allow(dead_code)] +impl VoterEligibility { + pub fn is_eligible(group: &AgeGroup) -> VoterEligibility { + match group { + AgeGroup::Juvenile => VoterEligibility::NotEligible, + _ => VoterEligibility::Eligible, + } + } + + pub fn description(&self) -> &str { + match self { + VoterEligibility::Eligible => "This voter is eligible.", + VoterEligibility::NotEligible => "You're not eligible to VOTE!", + } +} +} + +pub fn check_voter_eligibility(age: u32) { + let age_group = AgeGroup::from_age(age); + println!("You're in the age group of {:?}", age_group); +} diff --git a/rust_session/student_registry/Cargo.lock b/rust_session/student_registry/Cargo.lock index e7f8ee38..e33a95c8 100644 --- a/rust_session/student_registry/Cargo.lock +++ b/rust_session/student_registry/Cargo.lock @@ -2,6 +2,499 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "anyhow" +version = "1.0.102" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c" + +[[package]] +name = "bitflags" +version = "2.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4388bee8683e3d04af747c73422af53102d2bd24d9eadb6cbc100baef4b43f8" + +[[package]] +name = "bumpalo" +version = "3.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "foldhash" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" + +[[package]] +name = "futures-core" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d" + +[[package]] +name = "futures-task" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "037711b3d59c33004d3856fbdc83b99d4ff37a24768fa1be9ce3538a1cde4393" + +[[package]] +name = "futures-util" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "slab", +] + +[[package]] +name = "getrandom" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0de51e6874e94e7bf76d726fc5d13ba782deca734ff60d5bb2fb2607c7406555" +dependencies = [ + "cfg-if", + "libc", + "r-efi", + "wasip2", + "wasip3", +] + +[[package]] +name = "hashbrown" +version = "0.15.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" +dependencies = [ + "foldhash", +] + +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "id-arena" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954" + +[[package]] +name = "indexmap" +version = "2.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9" +dependencies = [ + "equivalent", + "hashbrown 0.17.1", + "serde", + "serde_core", +] + +[[package]] +name = "itoa" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" + +[[package]] +name = "js-sys" +version = "0.3.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2025f20d7a4fa7785846e7b63d10a76d3f1cee98ee5cb79ea59703f95e42162" +dependencies = [ + "cfg-if", + "futures-util", + "wasm-bindgen", +] + +[[package]] +name = "leb128fmt" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2" + +[[package]] +name = "libc" +version = "0.2.186" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66" + +[[package]] +name = "log" +version = "0.4.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "953f07c43838f8e6f9758cab68bf5bed85465e7587ebe0b823f1bcd81978ad3a" + +[[package]] +name = "memchr" +version = "2.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b947ae49db0d222b1dbc6b113ce7248a3fc3a6ca21b696717bfc000ba4484d8" + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "pin-project-lite" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" + +[[package]] +name = "prettyplease" +version = "0.2.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b" +dependencies = [ + "proc-macro2", + "syn", +] + +[[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 = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + +[[package]] +name = "rustversion" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" + +[[package]] +name = "semver" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" + +[[package]] +name = "serde" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.150" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8014e44b4736ed0538adeecded0fce2a272f22dc9578a7eb6b2d9993c74cfb9" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + [[package]] name = "student_registry" version = "0.1.0" +dependencies = [ + "uuid", +] + +[[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 = "unicode-xid" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" + +[[package]] +name = "uuid" +version = "1.23.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "144d6b123cef80b301b8f72a9e2ca4370ddec21950d0a103dd22c437006d2db7" +dependencies = [ + "getrandom", + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "wasip2" +version = "1.0.3+wasi-0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20064672db26d7cdc89c7798c48a0fdfac8213434a1186e5ef29fd560ae223d6" +dependencies = [ + "wit-bindgen 0.57.1", +] + +[[package]] +name = "wasip3" +version = "0.4.0+wasi-0.3.0-rc-2026-01-06" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5428f8bf88ea5ddc08faddef2ac4a67e390b88186c703ce6dbd955e1c145aca5" +dependencies = [ + "wit-bindgen 0.51.0", +] + +[[package]] +name = "wasm-bindgen" +version = "0.2.123" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a254a4b10c19a76f09a27640e7ffbf9bc30bf67e16a3bf28aaefa4920fe81563" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.123" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24a40fc75b0ec6f3746ceb10d36f53a93dcd68a93b11b6445983945d79eba0dc" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.123" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "908f34bd9b9ce3d4caf07b72dfab63d61504d156856c6bd3cd87fa350cf3985b" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.123" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7acbf7616c27b194bbb550bf77ed0c2c3e5b7fd1260a93082b95fb7f47959b92" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "wasm-encoder" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "990065f2fe63003fe337b932cfb5e3b80e0b4d0f5ff650e6985b1048f62c8319" +dependencies = [ + "leb128fmt", + "wasmparser", +] + +[[package]] +name = "wasm-metadata" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb0e353e6a2fbdc176932bbaab493762eb1255a7900fe0fea1a2f96c296cc909" +dependencies = [ + "anyhow", + "indexmap", + "wasm-encoder", + "wasmparser", +] + +[[package]] +name = "wasmparser" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47b807c72e1bac69382b3a6fb3dbe8ea4c0ed87ff5629b8685ae6b9a611028fe" +dependencies = [ + "bitflags", + "hashbrown 0.15.5", + "indexmap", + "semver", +] + +[[package]] +name = "wit-bindgen" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7249219f66ced02969388cf2bb044a09756a083d0fab1e566056b04d9fbcaa5" +dependencies = [ + "wit-bindgen-rust-macro", +] + +[[package]] +name = "wit-bindgen" +version = "0.57.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" + +[[package]] +name = "wit-bindgen-core" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea61de684c3ea68cb082b7a88508a8b27fcc8b797d738bfc99a82facf1d752dc" +dependencies = [ + "anyhow", + "heck", + "wit-parser", +] + +[[package]] +name = "wit-bindgen-rust" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7c566e0f4b284dd6561c786d9cb0142da491f46a9fbed79ea69cdad5db17f21" +dependencies = [ + "anyhow", + "heck", + "indexmap", + "prettyplease", + "syn", + "wasm-metadata", + "wit-bindgen-core", + "wit-component", +] + +[[package]] +name = "wit-bindgen-rust-macro" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c0f9bfd77e6a48eccf51359e3ae77140a7f50b1e2ebfe62422d8afdaffab17a" +dependencies = [ + "anyhow", + "prettyplease", + "proc-macro2", + "quote", + "syn", + "wit-bindgen-core", + "wit-bindgen-rust", +] + +[[package]] +name = "wit-component" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d66ea20e9553b30172b5e831994e35fbde2d165325bec84fc43dbf6f4eb9cb2" +dependencies = [ + "anyhow", + "bitflags", + "indexmap", + "log", + "serde", + "serde_derive", + "serde_json", + "wasm-encoder", + "wasm-metadata", + "wasmparser", + "wit-parser", +] + +[[package]] +name = "wit-parser" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecc8ac4bc1dc3381b7f59c34f00b67e18f910c2c0f50015669dde7def656a736" +dependencies = [ + "anyhow", + "id-arena", + "indexmap", + "log", + "semver", + "serde", + "serde_derive", + "serde_json", + "unicode-xid", + "wasmparser", +] + +[[package]] +name = "zmij" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa" diff --git a/rust_session/student_registry/Cargo.toml b/rust_session/student_registry/Cargo.toml index bde0b676..fbd75677 100644 --- a/rust_session/student_registry/Cargo.toml +++ b/rust_session/student_registry/Cargo.toml @@ -2,3 +2,6 @@ name = "student_registry" version = "0.1.0" edition = "2021" + +[dependencies] +uuid = { version = "1", features = ["v4"] } diff --git a/rust_session/student_registry/src/grade.rs b/rust_session/student_registry/src/grade.rs index 10438b1e..b253e8d0 100644 --- a/rust_session/student_registry/src/grade.rs +++ b/rust_session/student_registry/src/grade.rs @@ -22,10 +22,10 @@ pub enum Sex { } impl Sex { - pub fn to_str(&self) { + pub fn as_str(&self) -> &str { match self { - Sex::Male => println!("male: 👨🏾"), - Sex::Female => println!("female: 👧🏾"), + Sex::Male => "Male", + Sex::Female => "Female", } } } diff --git a/rust_session/student_registry/src/main.rs b/rust_session/student_registry/src/main.rs index 7ae18ff7..6721d68f 100644 --- a/rust_session/student_registry/src/main.rs +++ b/rust_session/student_registry/src/main.rs @@ -1,43 +1,166 @@ mod grade; mod registry; +mod registry_uuid; +mod registry_struct; mod student_struct; mod utils; use grade::{Grade, Sex}; use registry::Registry; -use student_struct::Student; +use registry_uuid::RegistryUUID; fn main() { - // let g = Grade::Second; - // println!("{}", g.as_str()); // "2nd Year" - // println!("{:?}", g); - - // let ss = Student::new(); - // println!("stund") - - // let mut reg = Registry::new(); - - // reg.add("Victor", 20, Grade::First, 78.5); - // reg.add("Kosi", 22, Grade::Second, 64.0); - // reg.add("Yusrah", 21, Grade::First, 91.0); - - // reg.list_all();] - - let sex = Sex::Male; - println!("sex: {:?}", sex.to_str()); - - // let s: Student = Student::new(1, String::from("Testimony"), 16, Sex::Female, Grade::Third, 40.5); - let s: Student = Student::new( - 1, - "Testimony".to_string(), - 16, - Sex::Female, - Grade::Third, - 40.5, - ); - println!("student here: {:#?}", s); - - println!("student id: {}", s.id); - println!("student name: {}", s.name); - println!("student age: {}", s.age); + // === Registry with u32 IDs + println!("Registry Table\n"); + + let mut registry = Registry::new(); + + match registry.add("Kingsman", 20, Sex::Female, Grade::First, 78.5) { + Ok(_) => {} + Err(e) => println!("Error: {}", e), + } + match registry.add("Jigan", 42, Sex::Female, Grade::Second, 64.0) { + Ok(_) => {} + Err(e) => println!("Error: {}", e), + } + match registry.add("Yusrah", 21, Sex::Female, Grade::First, 91.0) { + Ok(_) => {} + Err(e) => println!("Error: {}", e), + } + match registry.add("Testimony", 16, Sex::Female, Grade::Third, 40.5) { + Ok(_) => {} + Err(e) => println!("Error: {}", e), + } + + // invalid add - empty name + match registry.add("", 20, Sex::Male, Grade::First, 50.0) { + Ok(_) => {} + Err(e) => println!("Rejected: {}", e), + } + + // invalid add - bad age + match registry.add("Ghost", 0, Sex::Male, Grade::First, 50.0) { + Ok(_) => {} + Err(e) => println!("Rejected: {}", e), + } + + // invalid add - score out of range + match registry.add("Ghost", 25, Sex::Male, Grade::First, 120.0) { + Ok(_) => {} + Err(e) => println!("Rejected: {}", e), + } + + println!("\n All students "); + registry.list_all(); + + println!("\n Get student ID 2 "); + registry.get_student_by_id(2); + + println!("\n Update Jigan's age "); + match registry.update_age(2, 36) { + Ok(_) => {} + Err(e) => println!("Error: {}", e), + } + registry.get_student_by_id(2); + + // invalid age update + match registry.update_age(2, 0) { + Ok(_) => {} + Err(e) => println!("Rejected: {}", e), + } + + println!("\n Update Jigan's name "); + match registry.update_name(2, "Jigah".to_string()) { + Ok(_) => {} + Err(e) => println!("Error: {}", e), + } + registry.get_student_by_id(2); + + // invalid name update + match registry.update_name(2, " ".to_string()) { + Ok(_) => {} + Err(e) => println!("Rejected: {}", e), + } + + println!("\n Update Kingsman's sex "); + match registry.update_sex(1, Sex::Male) { + Ok(_) => {} + Err(e) => println!("Error: {}", e), + } + registry.get_student_by_id(1); + + println!("\n Update Kingsman's grade "); + match registry.update_grade(1, Grade::Second) { + Ok(_) => {} + Err(e) => println!("Error: {}", e), + } + registry.get_student_by_id(1); + + println!("\n Delete student ID 3 (Yusrah) "); + match registry.delete_student(3) { + Ok(_) => {} + Err(e) => println!("Error: {}", e), + } + registry.list_all(); + + // === Registry with UUID IDs + println!("\n\nRegistryUUID\n"); + + let mut uuid_registry = RegistryUUID::new(); + + match uuid_registry.add("Basongo", 23, Sex::Male, Grade::Second, 72.5) { + Ok(_) => {} + Err(e) => println!("Error: {}", e), + } + match uuid_registry.add("Faith", 19, Sex::Female, Grade::First, 88.0) { + Ok(_) => {} + Err(e) => println!("Error: {}", e), + } + match uuid_registry.add("Mac5", 20, Sex::Male, Grade::Third, 55.0) { + Ok(_) => {} + Err(e) => println!("Error: {}", e), + } + + // invalid add + match uuid_registry.add("", 20, Sex::Male, Grade::First, 50.0) { + Ok(_) => {} + Err(e) => println!("Rejected: {}", e), + } + + println!("\n All UUID students "); + uuid_registry.list_all(); + + let basongo_id = uuid_registry.students[0].id; + let faith_id = uuid_registry.students[1].id; + + println!("\n Update Basongo's name "); + match uuid_registry.update_name(basongo_id, "Ochuko, Isaac Onodairo".to_string()) { + Ok(_) => {} + Err(e) => println!("Error: {}", e), + } + uuid_registry.get_student_by_id(basongo_id); + + println!("\n Delete the first UUID student "); + match uuid_registry.delete_student(basongo_id) { + Ok(_) => {} + Err(e) => println!("Error: {}", e), + } + + println!("\n Get UUID of a student"); + uuid_registry.get_student_by_id(faith_id); + + println!("\n Update Faith's age "); + match uuid_registry.update_age(faith_id, 92) { + Ok(_) => {} + Err(e) => println!("Error: {}", e), + } + uuid_registry.get_student_by_id(faith_id); + + // invalid age update + match uuid_registry.update_age(faith_id, 0) { + Ok(_) => {} + Err(e) => println!("Rejected: {}", e), + } + + uuid_registry.list_all(); } diff --git a/rust_session/student_registry/src/registry.rs b/rust_session/student_registry/src/registry.rs index 146dc186..e95719ff 100644 --- a/rust_session/student_registry/src/registry.rs +++ b/rust_session/student_registry/src/registry.rs @@ -7,33 +7,111 @@ pub struct Registry { } impl Registry { - pub fn add(&mut self, name: &str, age: u8, sex: Sex, grade: Grade, score: f32) { + pub fn new() -> Registry { + Registry { + students: Vec::new(), + next_id: 1, + } + } + + // === Add student to registry + pub fn add(&mut self, name: &str, age: u8, sex: Sex, grade: Grade, score: f32) -> Result<(), String> { let id = self.next_id; - let student = Student::new(id, name.to_string(), age, sex, grade, score); + let student = Student::new(id, name.to_string(), age, sex, grade, score)?; println!("Added: {} (ID {})", student.name, student.id); self.students.push(student); self.next_id += 1; + Ok(()) } + // === List all students pub fn list_all(&self) { if self.students.is_empty() { println!(" (no students enrolled yet)"); return; } println!( - " {:>5} {:<20} {:<6} {:<10} {}", - "ID", "Name", "Age", "Grade", "Score" + " {:>5} {:<20} {:<6} {:<8} {:<10} {}", + "ID", "Name", "Age", "Sex", "Grade", "Score" ); - println!(" {}", "-".repeat(55)); + println!(" {}", "-".repeat(65)); for student in &self.students { println!( - " {:>5} {:<20} {:>6} {:<10} {:.1}", + " {:>5} {:<20} {:>6} {:<8} {:<10} {:.1}", student.id, student.name, student.age, + student.sex.as_str(), student.grade.as_str(), student.score, ); } } + + // === Get student by id + pub fn get_student_by_id(&self, id: u32) { + if let Some(student) = self.students.iter().find(|s| s.id == id) { + println!("{:?}", student); + } else { + println!("Student with ID {} not found", id); + } + } + + // === Update functions + pub fn update_age(&mut self, id: u32, new_age: u8) -> Result<(), String> { + if new_age == 0 || new_age > 100 { + return Err(format!("Invalid age: {}", new_age)); + } + if let Some(student) = self.students.iter_mut().find(|s| s.id == id) { + student.age = new_age; + println!("Updated age for student ID {}", id); + Ok(()) + } else { + Err(format!("Student with ID {} not found", id)) + } + } + + pub fn update_name(&mut self, id: u32, new_name: String) -> Result<(), String> { + if new_name.trim().is_empty() { + return Err("Name cannot be empty".to_string()); + } + if let Some(student) = self.students.iter_mut().find(|s| s.id == id) { + student.name = new_name; + println!("Updated name for student ID {}", id); + Ok(()) + } else { + Err(format!("Student with ID {} not found", id)) + } + } + + pub fn update_sex(&mut self, id: u32, new_sex: Sex) -> Result<(), String> { + if let Some(student) = self.students.iter_mut().find(|s| s.id == id) { + student.sex = new_sex; + println!("Updated sex for student ID {}", id); + Ok(()) + } else { + Err(format!("Student with ID {} not found", id)) + } + } + + pub fn update_grade(&mut self, id: u32, new_grade: Grade) -> Result<(), String> { + if let Some(student) = self.students.iter_mut().find(|s| s.id == id) { + student.grade = new_grade; + println!("Updated grade for student ID {}", id); + Ok(()) + } else { + Err(format!("Student with ID {} not found", id)) + } + } + + // === Delete by id function + pub fn delete_student(&mut self, id: u32) -> Result<(), String> { + if let Some(index) = self.students.iter().position(|s| s.id == id) { + self.students.remove(index); + println!("Deleted student ID {}", id); + Ok(()) + } else { + Err(format!("Student with ID {} not found", id)) + } + } } diff --git a/rust_session/student_registry/src/registry_struct.rs b/rust_session/student_registry/src/registry_struct.rs index 8b076a03..ce520e8e 100644 --- a/rust_session/student_registry/src/registry_struct.rs +++ b/rust_session/student_registry/src/registry_struct.rs @@ -1,5 +1,6 @@ use crate::student_struct; +#[allow(dead_code)] pub struct Registry { students: Vec, // Vec = "a list of Student values" next_id: u32, // auto-increment counter for IDs diff --git a/rust_session/student_registry/src/registry_uuid.rs b/rust_session/student_registry/src/registry_uuid.rs new file mode 100644 index 00000000..e34c1a22 --- /dev/null +++ b/rust_session/student_registry/src/registry_uuid.rs @@ -0,0 +1,115 @@ +use crate::grade::{Grade, Sex}; +use crate::student_struct::StudentV2; +use uuid::Uuid; + +pub struct RegistryUUID { + pub students: Vec, +} + +impl RegistryUUID { + pub fn new() -> RegistryUUID { + RegistryUUID { + students: Vec::new(), + } + } + + // === Add student to registry + pub fn add(&mut self, name: &str, age: u8, sex: Sex, grade: Grade, score: f32) -> Result<(), String> { + let student = StudentV2::new(name.to_string(), age, sex, grade, score)?; + println!("Added: {} with the following ID - {}", student.name, student.id); + self.students.push(student); + Ok(()) + } + + // === List all students + pub fn list_all(&self) { + if self.students.is_empty() { + println!(" (no students enrolled yet)"); + return; + } + println!( + " {:<38} {:<20} {:<6} {:<8} {:<10} {}", + "ID", "Name", "Age", "Sex", "Grade", "Score" + ); + println!(" {}", "-".repeat(95)); + for student in &self.students { + println!( + " {:<38} {:<20} {:>6} {:<8} {:<10} {:.1}", + student.id, + student.name, + student.age, + student.sex.as_str(), + student.grade.as_str(), + student.score, + ); + } + } + + // === Get student by UUID + pub fn get_student_by_id(&self, id: Uuid) { + if let Some(student) = self.students.iter().find(|s| s.id == id) { + println!("{:?}", student); + } else { + println!("Student with ID {} not found", id); + } + } + + // === Update fields + pub fn update_age(&mut self, id: Uuid, new_age: u8) -> Result<(), String> { + if new_age == 0 || new_age > 100 { + return Err(format!("Invalid age: {}", new_age)); + } + if let Some(student) = self.students.iter_mut().find(|s| s.id == id) { + student.age = new_age; + println!("Updated age for student ID {}", id); + Ok(()) + } else { + Err(format!("Student with ID {} not found", id)) + } + } + + pub fn update_name(&mut self, id: Uuid, new_name: String) -> Result<(), String> { + if new_name.trim().is_empty() { + return Err("Name cannot be empty".to_string()); + } + if let Some(student) = self.students.iter_mut().find(|s| s.id == id) { + student.name = new_name; + println!("Updated name for student ID {}", id); + Ok(()) + } else { + Err(format!("Student with ID {} not found", id)) + } + } + + #[allow(dead_code)] + pub fn update_sex(&mut self, id: Uuid, new_sex: Sex) -> Result<(), String> { + if let Some(student) = self.students.iter_mut().find(|s| s.id == id) { + student.sex = new_sex; + println!("Updated sex for student ID {}", id); + Ok(()) + } else { + Err(format!("Student with ID {} not found", id)) + } + } + + #[allow(dead_code)] + pub fn update_grade(&mut self, id: Uuid, new_grade: Grade) -> Result<(), String> { + if let Some(student) = self.students.iter_mut().find(|s| s.id == id) { + student.grade = new_grade; + println!("Updated grade for student ID {}", id); + Ok(()) + } else { + Err(format!("Student with ID {} not found", id)) + } + } + + pub fn delete_student(&mut self, id: Uuid) -> Result<(), String> { + if let Some(index) = self.students.iter().position(|s| s.id == id) { + self.students.remove(index); + println!("Deleted student ID {}", id); + Ok(()) + } else { + Err(format!("Student with ID {} not found", id)) + } + } +} diff --git a/rust_session/student_registry/src/student_struct.rs b/rust_session/student_registry/src/student_struct.rs index 9e4ead64..ab98f6a7 100644 --- a/rust_session/student_registry/src/student_struct.rs +++ b/rust_session/student_registry/src/student_struct.rs @@ -1,32 +1,71 @@ use crate::grade::{Grade, Sex}; - -// A struct groups related pieces of data under one name. -// Think of it as a custom data type you design yourself. +use uuid::Uuid; #[derive(Debug)] pub struct Student { - pub id: u32, // u32 = unsigned 32-bit integer (no negatives) - pub name: String, // String = heap-allocated, growable text + pub id: u32, + pub name: String, pub age: u8, pub sex: Sex, - pub grade: Grade, // our own enum type from above + pub grade: Grade, pub score: f32, } -// This is the implementation of the student struct with its corresponding methods impl Student { - pub fn new(id: u32, name: String, age: u8, sex: Sex, grade: Grade, score: f32) -> Student { - Student { + pub fn new(id: u32, name: String, age: u8, sex: Sex, grade: Grade, score: f32) -> Result { + if name.trim().is_empty() { + return Err("Name cannot be empty".to_string()); + } + if age == 0 || age > 100 { + return Err(format!("Invalid age: {}", age)); + } + if score < 0.0 || score > 100.0 { + return Err(format!("Score must be between 0 and 100, got {}", score)); + } + Ok(Student { id, - name, + name: name.trim().to_string(), age, sex, grade, score, + }) + } +} + +#[derive(Debug)] +pub struct StudentV2 { + pub id: Uuid, + pub name: String, + pub age: u8, + pub sex: Sex, + pub grade: Grade, + pub score: f32, +} + +impl StudentV2 { + pub fn new(name: String, age: u8, sex: Sex, grade: Grade, score: f32) -> Result { + if name.trim().is_empty() { + return Err("Name cannot be empty".to_string()); + } + if age == 0 || age > 100 { + return Err(format!("Invalid age: {}", age)); + } + if score < 0.0 || score > 100.0 { + return Err(format!("Score must be between 0 and 100, got {}", score)); } + Ok(StudentV2 { + id: Uuid::new_v4(), + name: name.trim().to_string(), + age, + sex, + grade, + score, + }) } } +#[allow(dead_code)] #[derive(Debug)] pub enum Status { Pending, @@ -34,9 +73,10 @@ pub enum Status { Completed, } +#[allow(dead_code)] pub struct Todo { - id: u8, - title: String, - description: String, - status: Status, + pub id: u8, + pub title: String, + pub description: String, + pub status: Status, } diff --git a/rust_session/student_registry/src/utils.rs b/rust_session/student_registry/src/utils.rs index ab2c8eeb..581c1646 100644 --- a/rust_session/student_registry/src/utils.rs +++ b/rust_session/student_registry/src/utils.rs @@ -1,3 +1,4 @@ +// #[allow(dead_code)] // pub fn to_str(x: String) -> &'static str { // x.as_str() // } diff --git a/rust_session/utils/utils.rs b/rust_session/utils/utils.rs new file mode 100644 index 00000000..b24558bc --- /dev/null +++ b/rust_session/utils/utils.rs @@ -0,0 +1,4 @@ +#[allow(dead_code)] +pub fn to_str(x: String) -> &str { + x.as_str() +}