打赏

[易学易懂系列|rustlang语言|零基础|快速入门|(14)|Impls & Traits实现与特征]

[易学易懂系列|rustlang语言|零基础|快速入门|(14)]

有意思的基础知识

Impls & Traits实现与特征

我之前说到的struct结构体,其实就类似于面向对象语言中的类class。

但这个struct,并没有定义方法或函数。

那要怎么办呢?

Rust用关键词impls(实现)来定义struct和enum的方法或函数。

而trait(特征),类似于面向对象语言中的接口interface。

特征,是用来定义要实现的方法,一个类型可以有多个特征。特征可以有默认实现函数,这个默认函数可以在运行时重写。

我们来看看代码:

1.没有trait特征的impl实现:

struct Player {
    first_name: String,
    last_name: String,
}

impl Player {
    
    fn full_name(&self) -> String {
        format!("{} {}", self.first_name, self.last_name)
    }
}

fn main() {
    let player_1 = Player {
        first_name: "Rafael".to_string(),
        last_name: "Nadal".to_string(),
    };

    println!("Player 01: {}", player_1.full_name());
}

// ⭐️ Implementation must appear in the same crate as the self type

// 💡 And also in Rust, new traits can be implemented for existing types even for types like i8, f64 and etc.
// Same way existing traits can be implemented for new types you are creating.
// But we can not implement existing traits into existing types.

我们来看看上面的代码,其中这段代码,跟之前的函数,有点不一样:

impl Player {
    fn full_name(&self) -> String {
        format!("{} {}", self.first_name, self.last_name)
    }
}

这里第一个参数:&self,它代表什么意思呢?

&self代表结构体Player的实例。

当然这里的第一个参数,可以是:self, &self, 或&mut self

self是一个栈内值 。

&self是一个引用值(不可变),数据在堆heap里分配空间。

&mut self是一个可变的引用值,数据在堆heap里分配空间

所以通过,self.first_name, self.last_name,我们就可以访问结构体的属性first_name,last_name。

这里的impl 代码块代表结构体Player的具体实现方法,impl后面的类型,必须是Player。

1.特征(trait)的实现方式:

struct Player {
    first_name: String,
    last_name: String,
}

trait FullName {
    fn full_name(&self) -> String;
}

impl FullName for Player {
    fn full_name(&self) -> String {
        format!("{} {}", self.first_name, self.last_name)
    }
}

fn main() {
    let player_2 = Player {
        first_name: "Roger".to_string(),
        last_name: "Federer".to_string(),
    };

    println!("Player 02: {}", player_2.full_name());
}

// 🔎 Other than functions, traits can contain constants and types.

我们可以看到上面代码中加了一个特征的定义,代码可读性好很多:

trait FullName {
    fn full_name(&self) -> String;
}

好,现在我们加上默认函数(default method):

struct Player {
    first_name: String,
    last_name: String,
}
trait FullName {
    fn full_name(&self) -> String;
    fn baz(&self) { println!("We called baz.");// 默认函数
}
impl FullName for Player {
    fn full_name(&self) -> String {
        format!("{} {}", self.first_name, self.last_name)
    }
}

fn main() {
    let player_2 = Player {
        first_name: "Roger".to_string(),
        last_name: "Federer".to_string(),
    };

    println!("Player 02: {}", player_2.full_name());
    player_2.baz();//直接调用默认函数
}

3.关联函数(Associated functions):

请看如下例子:

struct Player {
    first_name: String,
    last_name: String,
}

impl Player {
    fn new(first_name: String, last_name: String) -> Player {
        Player {
            first_name : first_name,
            last_name : last_name,
        }
    }

    fn full_name(&self) -> String {
        format!("{} {}", self.first_name, self.last_name)
    }
}

fn main() {
    let player_name = Player::new("Serena".to_string(), "Williams".to_string()).full_name();
    println!("Player: {}", player_name);
}

// We have used :: notation for `new()` and . notation for `full_name()`

// 🔎 Also in here, instead of using new() and full_name() separately as two expressions, 
// we can use Method Chaining. ex. `player.add_points(2).get_point_count();`

上面代码,我们看到多了一个函数new(注意这个函数的参数,不用self关键字),我们看到这个new函数跟java中的构造函数很像。

这个函数可以不通过创建对象实例,就可以直接调用,一般用双冒号::,如:Player::new。

在Rust,我们叫这种函数为:关联函数(Associated functions)。

我们再来看看特征的其他用法:

带泛型的特征:

trait From<T> {
    fn from(T) -> Self;
}
    impl From<u8> for u16 {
        //...
    }
    impl From<u8> for u32{
        //...
    }

// Should specify after the trait name like generic functions

带继承关系的特征:

trait Person {
    fn full_name(&self) -> String;
}
trait Expat{
    fn tax(&self)->f64;
}

    trait Employee : Person { // Employee inherits from person trait
      fn job_title(&self) -> String;
    }

    trait ExpatEmployee : Employee + Expat { // ExpatEmployee inherits from Employee and Expat traits
      fn additional_tax(&self) -> f64;
    }

特征对象:

trait GetSound {
    fn get_sound(&self) -> String;
}

struct Cat {
    sound: String,
}
    impl GetSound for Cat {
        fn get_sound(&self) -> String {
            self.sound.clone()
        }
    }

struct Bell {
    sound: String,
}
    impl GetSound for Bell {
        fn get_sound(&self) -> String {
            self.sound.clone()
        }
    }


fn make_sound<T: GetSound>(t: &T) {
    println!("{}!", t.get_sound())
}

fn main() {
    let kitty = Cat { sound: "Meow".to_string() };
    let the_bell = Bell { sound: "Ding Dong".to_string() };

    make_sound(&kitty); // Meow!
    make_sound(&the_bell); // Ding Dong!
}

上面的代码我们可以看到,Rust实现多态,可以用动态分发的机制,所谓动态分发,就是在运行时,才决定真正的实现,并加载这个实现。

以上,希望对你有用。

如果遇到什么问题,欢迎加入:rust新手群,在这里我可以提供一些简单的帮助,加微信:360369487,注明:博客园+rust

参考:https://doc.rust-lang.org/rust-by-example/generics/impl.html

https://learning-rust.github.io/docs/b5.impls_and_traits.html

https://doc.rust-lang.org/book/ch05-03-method-syntax.html

posted @ 2019-12-02 11:26  gyc567  阅读(425)  评论(0编辑  收藏  举报