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());
}