[initial] first edition
This commit is contained in:
commit
0613f04ed2
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/target
|
||||
83
Cargo.lock
generated
Normal file
83
Cargo.lock
generated
Normal file
@ -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"
|
||||
7
Cargo.toml
Normal file
7
Cargo.toml
Normal file
@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "course_2"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
email_address = "0.2.9"
|
||||
111
src/main.rs
Normal file
111
src/main.rs
Normal file
@ -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<T: FromStr>(prompts: &str) -> T
|
||||
where
|
||||
<T as FromStr>::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::<T>() {
|
||||
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<Self, Self::Err> {
|
||||
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<Student> {
|
||||
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::<String>("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)
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user