diff --git a/BasicSyntax/structtest/Cargo.toml b/BasicSyntax/structtest/Cargo.toml new file mode 100644 index 0000000..a7af6f0 --- /dev/null +++ b/BasicSyntax/structtest/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "structtest" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/BasicSyntax/structtest/src/main.exe b/BasicSyntax/structtest/src/main.exe new file mode 100644 index 0000000..4abfbe5 Binary files /dev/null and b/BasicSyntax/structtest/src/main.exe differ diff --git a/BasicSyntax/structtest/src/main.pdb b/BasicSyntax/structtest/src/main.pdb new file mode 100644 index 0000000..1ef068f Binary files /dev/null and b/BasicSyntax/structtest/src/main.pdb differ diff --git a/BasicSyntax/structtest/src/main.rs b/BasicSyntax/structtest/src/main.rs new file mode 100644 index 0000000..dcfdcc9 --- /dev/null +++ b/BasicSyntax/structtest/src/main.rs @@ -0,0 +1,54 @@ +enum Class{ + AC, + BC, + CC, + DC +} + +struct Student{ + name: String, + age: i64, + score: f32, + class: Class +} + +trait Action{ + //接口类 + fn show_age(&self) -> i64 ; + fn show_class(&self); +} + +impl Action for Student{ + fn show_age(&self) -> i64 { + return self.age; + } + fn show_class(&self){ + match self.class{ + Class::AC => println!("Class A"), + Class::BC => println!("Class B"), + Class::CC => println!("Class C"), + Class::DC => println!("Class D"), + _ => println!("No Class!") + } + } +} + +impl Student{ + fn show_score(&self) -> f32 { + return self.score; + } + fn prt(&self){ + println!("Name: {0}, Age: {1}, Score: {2}", self.name, self.age, self.score); + } +} +fn main() { + let s= Student{ + name : "Van".to_string(), + age : 18, + score : 99.9, + class : Class::AC + }; + println!("{}", s.show_score()); + println!("{}", s.show_age()); + s.prt(); +}