首先定义两个结构体 Dog 和 Cat 分别实现 Animal trait
trait Animal {
fn speak(&self);
}
struct Dog;
impl Animal for Dog {
fn speak(&self) {
println!("旺旺.....");
}
}
struct Cat;
impl Animal for Cat {
fn speak(&self) {
println!("喵喵.....");
}
}
通过泛型实现静态分发
fn animal_speak<T: Animal>(animal: T) {
animal.speak();
}
fn main() {
let dog = Dog;
let cat = Cat;
animal_speak(dog);
animal_speak(cat);
}
// 输出:
// 旺旺.....
// 喵喵.....
这样相当于为每个Dog和Cat分别实现了一个animal_speak()方法
fn dog_speak(dog: dog) {
dog.speak();
}
fn cat_speak(cat: Cat) {
cat.speak();
}
目前只能看出来静态分发会导致编译后的二进制文件膨胀,至于好处还要和动态分发对比才能知道;
通过&或者Box智能指针来实现动态分发
fn animal_speak(animal: &dyn Animal) {
animal.speak();
}
fn main() {
let dog = Dog;
let cat = Cat;
animal_speak(&dog);
animal_speak(&cat);
}
// 输出:
// 旺旺.....
// 喵喵.....
标签:分发,静态,dog,Cat,Rust,animal,cat,fn,speak
From: https://www.cnblogs.com/liuzonglin/p/17724711.html