Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

trait 函数集合,类似java中的interface接口

#![allow(unused)]
fn main() {
struct User{
    name:String,
    age:i8
}

trait Hello{
    fn hello(&self) -> String;
    fn print(&self){
        print!("hello trait");
    }
}

impl Hello for User{
    fn hello(&self) -> String {
        format!("hello {}",self.name)
    }
}

#[test]
fn trait_test(){
    let u = User{name:String::from("zhangsan"),age:18};
    u.print();
}
}