Structtest
This commit is contained in:
		
							
								
								
									
										6
									
								
								BasicSyntax/structtest/Cargo.toml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								BasicSyntax/structtest/Cargo.toml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
[package]
 | 
			
		||||
name = "structtest"
 | 
			
		||||
version = "0.1.0"
 | 
			
		||||
edition = "2024"
 | 
			
		||||
 | 
			
		||||
[dependencies]
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								BasicSyntax/structtest/src/main.exe
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								BasicSyntax/structtest/src/main.exe
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								BasicSyntax/structtest/src/main.pdb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								BasicSyntax/structtest/src/main.pdb
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										54
									
								
								BasicSyntax/structtest/src/main.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								BasicSyntax/structtest/src/main.rs
									
									
									
									
									
										Normal file
									
								
							@@ -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();
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user