Rust代码示例——16.2.使用dyn动态返回实现trait的类型

Rust编译器需要知道每个函数返回的类型需要多少内存空间。这就意味着你所有的函数都必须返回具体的类型。和其他编程语言不通,如果你有一个像Animal的接口,你不能写一个函数直接返回Animal,因为它的不同实现需要不同的内存空间。

但是有一个变通的方法,不是直接返回该接口对象,函数返回一个Box对象,它包含了Animal。Box只是对保存在堆上内存的一个引用。因为引用可以明确知道它的内存大小,编译器可以保证它指向在堆上分配给Animal的位置,这样我们就可以从函数中返回接口了。

一旦知道了堆上内存的地址,Rust就可以弄清楚了。因此如果你的函数以这种方式返回一个指向堆上接口的指针,你需要在返回的类型签名增加dyn关键字,即:Box<dyn Animal>。

struct Sheep {}

struct Cow {}


trait Animal {

    // Instance method signature

    fn noise(&self) -> &'static str;

}


// Implement the `Animal` trait for `Sheep`.

impl Animal for Sheep {

    fn noise(&self) -> &'static str {

        "baaaaah!"

    }

}


// Implement the `Animal` trait for `Cow`.

impl Animal for Cow {

    fn noise(&self) -> &'static str {

        "moooooo!"

    }

}


// Returns some struct that implements Animal, but we don't know which one at compile time.

fn random_animal(random_number: f64) -> Box<dyn Animal> {

    if random_number < 0.5 {

        Box::new(Sheep {})

    } else {

        Box::new(Cow {})

    }

}


fn main() {

    let random_number = 0.234;

    let animal = random_animal(random_number);

    println!("You've randomly chosen an animal, and it says {}", animal.noise());

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值