Rust 程式設計影片教程(進階)——002 trait

linghuyichong發表於2020-01-08

頭條地址:https://www.ixigua.com/i677586170644791348...
B站地址:https://www.bilibili.com/video/av81202308/

1、trait用於定義與其它型別共享的功能,類似於其它語言中的介面。
(1)可以透過trait以抽象的方式定義共享的行為。
(2)可以使用trait bounds指定泛型是任何擁有特定行為的型別。

2、定義trait

pub trait GetInformation{
    fn get_name(&self) -> &String;
    fn get_age(&self) -> u32;
}

3、實現trait

//-----------------------------------------
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
    }
}
//----------------------------------------
pub struct Teacher {
    pub name: String,
    pub age: u32,
}
impl GetInformation for Teacher {
    fn get_name(&self) -> &String {
        &self.name
    }
    fn get_age(&self) -> u32 {
        self.age
    }
}

4、預設實現:可以在定義trait的時候提供預設的行為,trait的型別可以使用預設的行為。
例子:

trait SchoolName {
    fn get_school_name(&self) -> String {
        String::from("HongXingSchool")
    }
}

//下面使用預設實現
pub struct Teacher {
    pub name: String,
    pub age: u32,
}

impl SchoolName for Teacher {}
fn main() {
    let t = Teacher{ name: String::from("andy"), age: 32};
    let t_school_name = t.get_school_name();
    println!("teacher school name = {}", t_school_name);
}

5、trait作為引數
例子:

pub fn print_information(item: impl GetInformation) {
    println!("name = {}", item.get_name());
    println!("age = {}", item.get_age());
}

6、trait bound語法
上面的例子可以寫成:

pub fn print_information<T: GetInformation>(item: T) {
    println!("name = {}", item.get_name());
    println!("age = {}", item.get_age());
}

7、透過+指定多個trait bound

pub trait GetName {
    fn get_name(&self) -> &String;
}

pub trait GetAge {
    fn get_age(&self) -> u32;
}

//寫法一
pub fn print_information<T: GetName+GetAge>(item: T) {
    println!("name = {}", item.get_name());
    println!("age = {}", item.get_age());
}

//寫法二,使用where
pub fn print_information<T>(item: T)
    where T: GetName+GetAge 
{
    println!("name = {}", item.get_name());
    println!("age = {}", item.get_age());
}

8、返回trait的型別

fn produce_item_with_information() -> impl GetInformation {
    Teacher {
        name: String::from("Andy"),
        age: 32,
    }
}

但是,需要注意的是,只能返回單一型別,以下實現會出錯(因為返回了兩個型別):

fn produce_item_with_information(is_teacher: bool) -> impl GetInformation {
    if is_teacher {
        Teacher {
            name: String::from("Andy"),
            age: 32,
        }
    } else {
        Student {
            name: String::from("harden"),
            age: 47,
        }
    }
}

9、複習之前的largest例子

fn largest<T:PartialOrd + Copy>(list: &[T]) -> T { //注意,要實現比較和複製的trait才行,否則報錯
    let mut largest = list[0];
    for &item in list.iter() {
        if item > largest {
            largest = item;
        }
    }
    largest
}

fn main(){
    let number_list = vec![1, 2, 22, 3, 42];
    let r1 = largest_i32(&number_list);
    println!("r1 = {}", r1);
    let char_list = vec!['a', 'y', 'c', 'd'];
    let r2 = largest_char(&char_list);
    println!("r2 = {}", r2);
}

10、使用trait bound有條件地的實現方法

pub trait GetName {
    fn get_name(&self) -> &String;
}

pub trait GetAge {
    fn get_age(&self) -> u32;
}

struct PeopleMatchInformation<T, U> {
    master: T,
    employee: U,
}

impl <T: GetName+GetAge , U: GetName+GetAge> PeopleMatchInformation<T, U> {
    fn print_all_information(&self) {
        println!("teacher name = {}", self.master.get_name());
        println!("teacher age = {}", self.master.get_age());
        println!("student name = {}", self.employee.get_name());
        println!("student age = {}", self.employee.get_age());
    }
}

//使用
pub struct Teacher {
    pub name: String,
    pub age: u32,
}

impl GetName for Teacher {
    fn get_name(&self) -> &String {
        &(self.name)
    }
}

impl GetAge for Teacher {
    fn get_age(&self) -> u32{
        self.age
    }
}

pub struct Student{
    pub name: String,
    pub age: u32,
}

impl GetName for Student {
    fn get_name(&self) -> &String {
        &(self.name)
    }
}

impl GetAge for Student {
    fn get_age(&self) -> u32{
        self.age
    }
}

fn main() {
    let t = Teacher{ name: String::from("andy"), age: 32};
    let s = Student {name: String::from("harden"), age: 47};
    let m = PeopleMatchInformation{master: t, employee: s};
    m.print_all_information();
}

11、對任何實現了特定trait的型別有條件的實現trait
例如:

pub trait GetName {
    fn get_name(&self) -> &String;
}

pub trait PrintName {
    fn print_name(&self) ;
}

impl<T: GetName> PrintName for T {
    fn print_name(&self) {
        println!("name = {}", self.get_name());
    }
}

//----------使用----------------
pub struct Student{
    pub name: String,
}

impl GetName for Student {
    fn get_name(&self) -> &String {
        &(self.name)
    }
}

fn main() {
    let s = Student{name: String::from("Andy")};
    s.print_name(); //student實現了GetName trait,因此可是直接使用PrintName trait中的函式print_name
}
//引申:這種方式類似於c++裡面的繼承
本作品採用《CC 協議》,轉載必須註明作者和本文連結
令狐一衝

相關文章