惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
小众软件
小众软件
人人都是产品经理
人人都是产品经理
美团技术团队
Blog — PlanetScale
Blog — PlanetScale
S
SegmentFault 最新的问题
博客园 - 【当耐特】
V
V2EX
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 叶小钗
Google DeepMind News
Google DeepMind News
量子位
罗磊的独立博客
月光博客
月光博客
N
Netflix TechBlog - Medium
大猫的无限游戏
大猫的无限游戏
博客园_首页
P
Proofpoint News Feed
Jina AI
Jina AI
云风的 BLOG
云风的 BLOG
博客园 - 司徒正美
腾讯CDC

博客园 - liufu627

使用option 链式调用来代替异步方法幂调用 Rust 各种情况下如何修改变量 Rust 单链表的实现 Closure move 数值类型与引用类型的区别 # Rust异步网络编程 Rust学习 红黑树新解(删除) 红黑树新解(插入) WCF快速創建 dropdownlist - liufu627 - 博客园 ArcGIS安装 泛型OraHelper - liufu627 - 博客园 让SendKeys支持空格键 - liufu627 - 博客园 标点符号的英文说法 Hashtable 让哈希表顺序输出。 PDA datagrid问题 日期格式化 - liufu627 - 博客园 JSON.net的使用 关于C#测试Oracle数据库链接的问题
rust 使用泛型来完成多态
liufu627 · 2020-03-24 · via 博客园 - liufu627

Posted on 2020-03-24 22:54  liufu627  阅读(1208)  评论(0)    收藏  举报

trait Bird {

    fn fly(&self);

  }

  struct Duck{x:i32}

  struct Swan{x:i64}

  impl Bird for Duck {

    fn fly(&self) { println!("duck duck"); }

  }

  impl Bird for Swan {

    fn fly(&self) { println!("swan swan");}

  }

  fn test<T: Bird>(arg: T) {

    arg.fly();

  }

  fn test2(arg: Box<Bird>) {

    arg.fly();

  }

pub fn _main(){   

    test(Duck{x:10});

    test(Swan{x:10});

    test2( Box::new(Duck{x:10}));

    test2( Box::new(Swan{x:10}));

}