From 0613f04ed2d4e53647a7dbf5a7750a6076062643 Mon Sep 17 00:00:00 2001 From: YinMo19 Date: Thu, 20 Nov 2025 16:05:23 +0800 Subject: [PATCH] [initial] first edition --- .gitignore | 1 + Cargo.lock | 83 +++++++++++++++++++++++++++++++++++++++ Cargo.toml | 7 ++++ src/main.rs | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 202 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs 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..bcd6467 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,83 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "course_2" +version = "0.1.0" +dependencies = [ + "email_address", +] + +[[package]] +name = "email_address" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e079f19b08ca6239f47f8ba8509c11cf3ea30095831f7fed61441475edd8c449" +dependencies = [ + "serde", +] + +[[package]] +name = "proc-macro2" +version = "1.0.103" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f" +dependencies = [ + "proc-macro2", +] + +[[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 = "syn" +version = "2.0.110" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a99801b5bd34ede4cf3fc688c5919368fea4e4814a4664359503e6015b280aea" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..32ad394 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "course_2" +version = "0.1.0" +edition = "2024" + +[dependencies] +email_address = "0.2.9" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..bfe4278 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,111 @@ +use email_address::EmailAddress; +use std::{fmt::Debug, str::FromStr}; + +/// Data in Gender represents age. +enum Gender { + Male(u8), + Female(u8), +} + +#[allow(unused)] +struct Student { + name: String, + gender: Gender, + grade: u8, + email: EmailAddress, +} + +fn get_line() -> String { + let mut input = String::new(); + std::io::stdin() + .read_line(&mut input) + .expect("Failed to read line"); + input +} + +fn from_prompt(prompts: &str) -> T +where + ::Err: Debug, +{ + loop { + // print or reprint the prompts + println!("{}", prompts); + + // get the original line input + let input = get_line(); + + // parse the input to the generic type + match input.trim().parse::() { + Ok(value) => break value, + Err(err) => { + println!("Invalid input: {:?}, please input again.", err); + continue; + } + } + } +} + +impl FromStr for Gender { + type Err = String; + fn from_str(input: &str) -> Result { + let parts: Vec<&str> = input.trim().split_whitespace().collect(); + + if parts.len() != 2 { + return Err("i nvalid input".to_string()); + } + + let age: u8 = match parts[1].parse() { + Ok(age) => age, + Err(_) => return Err("invalid age".to_string()), + }; + + match parts[0].to_lowercase().as_str() { + "m" => Ok(Gender::Male(age)), + "f" => Ok(Gender::Female(age)), + _ => Err("invalid gender".to_string()), + } + } +} + +fn get_students() -> Vec { + let mut students = Vec::new(); + loop { + let student = Student { + name: from_prompt("please input student's name:"), + gender: from_prompt("please input student's gender (M/F) and age:"), + grade: from_prompt("please input student's grade:"), + email: from_prompt("please input student's email:"), + }; + + students.push(student); + + if from_prompt::("Do you want to quit?") + .trim() + .to_ascii_lowercase() + == "quit" + { + return students; + } + } +} + +fn main() { + let students = get_students(); + + students.iter().for_each(|student| match student.gender { + Gender::Male(age) => { + if age > 16 { + println!("To {}, please clean the floor.", student.name) + } else { + println!("To {}, please throw the trash.", student.name) + } + } + Gender::Female(age) => { + if age > 16 { + println!("To {}, please clean the blackboard.", student.name) + } else { + println!("To {}, please clean the desk.", student.name) + } + } + }); +}