use std::fmt::Debug;
 
pub trait GetInformation {
    fn get_name(&self) -> &String;
    fn get_age(&self) -> u32;
}
 
#[derive(Debug)]
pub struct Student {
    pub name: String,
    pub age: u32,
}
 
// impl GetInformation for Student {
//     fn get_name(&self) ->&String {
//         &self.name
//     }
    
//     fn get_age(&self) -> u32 {
//         self.age
//     }
// }
 
impl GetName for Student {
    fn get_name(&self) ->&String {
        &self.name
    }
}
 
impl GetAge for Student {
    fn get_age(&self) -> u32 {
        self.age
    }
}
 
#[derive(Debug)]
pub struct Teacher {
    pub name: String,
    pub age: u32,
    pub subject: String,
}
 
// impl GetInformation for Teacher {
//     fn get_name(&self) ->&String {
//         &self.name
//     }
    
//     fn get_age(&self) -> u32 {
//         self.age
//     }
// }
 
impl GetName for Teacher {
    fn get_name(&self) ->&String {
        &self.name
    }
}
 
impl GetAge for Teacher {
    fn get_age(&self) -> u32 {
        self.age
    }
}
//1、参数方式1
fn print_information1(item: &impl GetInformation) {
    println!("name = {}",item.get_name());
    println!("age = {}", item.get_age());
}
//2、参数方式2
fn print_information2<T: GetInformation>(item:&T) {
    println!("name = {}",item.get_name());
    println!("age = {}", item.get_age());
}
//3、参数方式3
fn print_information3<T: GetName+GetAge>(item:&T) {
    println!("name = {}",item.get_name());
    println!("age = {}", item.get_age());
}
 
//4、参数方式4
fn print_information4<T>(item:&T) 
    where T: GetName+GetAge
{
    println!("name = {}",item.get_name());
    println!("age = {}", item.get_age());
}
 
//返回值
fn produce_item_with_age() -> impl GetAge {
    Student {
       name: String::from("wzg"),
       age: 15,
    }
}
trait SchoolName {
    fn get_school_name(&self) -> String{
        String::from("HongXingSchool")
    }
}
 
impl SchoolName for Student{}
 
impl SchoolName for Teacher{
    fn get_school_name(&self) -> String{
        String::from("ChongWenLu school")
    }
}
 
pub trait GetName : std::fmt::Debug{
    fn get_name(&self) -> &String;
}
 
pub trait GetAge : std::fmt::Debug{
    fn get_age(&self) -> u32;
}

trait PrintName {
    fn print_name(&self);
}
//实现特定接口的方法    凡是实现了GetName接口的对象,就可以使用这个打印方法
impl <T: GetName> PrintName for T {
    fn print_name(&self) {
        println!("name = {}",self.get_name());
    }
}

struct PeopleMatchInformation<T,U> {
    tt:T,
    ss: U,
}

impl<T: GetName + GetAge, U: GetName + GetAge> PeopleMatchInformation<T,U>{
    fn print_all_information(&self) {
        println!("mast name = {}, student age = {}",self.tt.get_name(),self.ss.get_age());
    }
} 
 
fn main() {
	let s = Student{name:"wyn".to_string(),age: 35};
	let t = Teacher{name:"wzg".to_string(),age: 59,subject:"jsj".to_string()};
	println!("student name = {},teacher age = {}",s.get_name(),t.get_age());
	print_information4(&s);
	print_information4(&t);
	println!("{}" , s.get_school_name());
	println!("{}" , t.get_school_name());
 
    let obj = produce_item_with_age();
    println!("{:?}",obj);
    
    s.print_name();
    t.print_name();

    let p = PeopleMatchInformation{tt:t,ss:s};
    p.print_all_information();
}

Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐