pub trait Summary {
fn summarize_author(&self) -> String; fn summarize(&self) -> String {
format!("(Default Read more from {}...)", self.summarize_author())
}
} pub struct NewsArticle {
pub headline: String,
pub location: String,
pub author: String,
pub content: String,
} pub struct Tweet {
pub username: String,
pub content: String,
pub reply: bool,
pub retweet: bool,
} impl Summary for Tweet {
fn summarize_author(&self) -> String {
format!("@{}", self.username)
}
} impl Summary for NewsArticle {
fn summarize_author(&self) -> String {
format!("@{}", self.author)
} fn summarize(&self) -> String {
format!("(NewsArticle Read more from {}...)", self.summarize_author())
}
} impl Summary for String {
fn summarize_author(&self) -> String {
format!("@{}", &self)
} fn summarize(&self) -> String {
format!("(Read more from {}...)", self.summarize_author())
}
} impl Summary for i64 {
fn summarize_author(&self) -> String {
format!("@{}", &self)
} fn summarize(&self) -> String {
format!("(Read more from {}...)", self.summarize_author())
}
} pub fn notify(item: impl Summary) {
println!("Breaking news! {}", item.summarize());
} fn main(){
let article = NewsArticle {
headline: String::from("Penguins win the Stanley Cup Championship!"),
location: String::from("Pittsburgh, PA, USA"),
author: String::from("Iceburgh"),
content: String::from("The Pittsburgh Penguins once again are the best
hockey team in the NHL."),
}; println!("New article available! {}", article.summarize());
notify(article); let tweet = Tweet {
username: String::from("horse_ebooks"),
content: String::from("of course, as you probably already know, people"),
reply: false,
retweet: false,
}; println!("1 new tweet: {}", tweet.summarize());
notify(tweet);
notify(1000);
notify(String::from("你好,特质,你很强")); }

  

使用trait,语言表达很强啊。  越来越喜欢它了。

总结: 任何类型都可以实现特质。 类似于接口。

rust 神奇的特质的更多相关文章

  1. Rust到底值不值得学--Rust对比、特色和理念

    前言 其实我一直弄不明白一点,那就是计算机技术的发展,是让这个世界变得简单了,还是变得更复杂了. 当然这只是一个玩笑,可别把这个问题当真. 然而对于IT从业者来说,这可不是一个玩笑.几乎每一次的技术发 ...

  2. Rust初步(六):在C#中使用Rust组件

    上一篇文章,我们通过实例比较了一下C#和Rust的性能表现,应该说在Release模式下面,Rust进行计算密集型的运算还是有些比较明显的优势的.那么,我们有没有可能,在C#中做一些快速应用开发,而一 ...

  3. Rust语言学习笔记(7)

    模块 // 兄弟模块 mod network { fn connect() { } } mod client { fn connect() { } } // 父子模块 mod network { fn ...

  4. Scala入门3(特质线性化)

    尝试设计一套特质,灵活的改动整数队列.队列有两种操作:put把整数放入队列,get从尾部取出它们.队列是先进先出的,get应该依照入队列的顺序取数据.提示:可以用mutable.ArrayBuffer ...

  5. 刷完欧拉计划中难度系数为5%的所有63道题,我学会了Rust中的哪些知识点?

    我为什么学Rust? 2019年6月18日,Facebook发布了数字货币Libra的技术白皮书,我也第一时间体验了一下它的智能合约编程语言MOVE,发现这个MOVE是用Rust编写的,看来想准确理解 ...

  6. 通过欧拉计划学Rust(第1~6题)

    最近想学习Libra数字货币的MOVE语言,发现它是用Rust编写的,看来想准确理解MOVE的机制,还需要对Rust有深刻的理解,所以开始了Rust的快速入门学习. 看了一下网上有关Rust的介绍,都 ...

  7. rust 生命周期2

    之前定义的结构体,都是不含引用的. 如果想定义含引用的结构体,请定义生命周期注解 #[warn(unused_variables)] struct ImportantExcerpt<'a> ...

  8. Rust之路(3)——数据类型 下篇

    [未经书面同意,严禁转载] -- 2020-10-14 -- 架构是道,数据是术.道可道,非常道:术不名,不成术!道无常形,术却可循规. 学习与分析数据类型,最基本的方法就是搞清楚其存储原理,变量和对 ...

  9. 实践解析丨Rust 内置 trait:PartialEq 和 Eq

    摘要:Rust 在很多地方使用了 traits, 从非常浅显的操作符重载, 到 Send, Sync 这种非常微妙的特性. Rust 在很多地方使用了 traits, 从非常浅显的操作符重载, 到 S ...

随机推荐

  1. MyBatis—— org.apache.ibatis.binding.BindingException: Type interface com.web.mybatis.i.PersonMapper is not known to the MapperRegistry.

    报错信息: Exception in thread "main" org.apache.ibatis.binding.BindingException: Type interfac ...

  2. 一文带你了解nginx基础

    学习nginx,就要先了解什么是nginx,为什么使用nginx,最后才是了解怎么使用nginx nginx简介 nginx安装 一.Linux中安装nginx 二.Docker中安装nginx 三. ...

  3. 【转】B树、B+树、B*树、R 树、QuadTree

    从B树.B+树.B*树谈到R 树 QuadTree(四叉树) 四叉树空间索引原理及其实现

  4. secureCRT连接liunx(centos6.5)系统步骤以及碰见的问题

    1.首先安装secureCRT以及用vmware安装centos6.5系统,用vmware打开centos6.5系统 2.找到liunx系统的ip,在liunx终端用ifconfig找到ip如下图: ...

  5. PIX防火墙配置A/S故障切换

    PIX防火墙配置A/S故障切换 1.基本命令 failover show failover failover lan enable failover lan interface zwish e2 fa ...

  6. 前端开发SEO的理解

    所谓seo(Search Engine Optimization)即搜索引擎优化.简单说就是百度.谷歌搜索引擎的‘蜘蛛’,如下图: 搜索引擎蜘蛛是通过,连接地址来找到你的网站的,seo就是让你的网站符 ...

  7. Flutter,webview里面实现上传和下载的功能

    前提:Flutter 与 webview(vue) 一起开发的项目 开始的时候并没有想到什么移动端的,所以上传就用input,下载就用iframe来实现,然而真机实测的时候,input那个方法IOS支 ...

  8. 关于vue+element对ie9的兼容el-upload不支持在IE9上传

    关于vue+element对ie9的兼容el-upload不支持在IE9上传 https://lian-yue.github.io/vue-upload-component/#/zh-cn/ 解决方案 ...

  9. Rocket - diplomacy - 模块结构信息

    https://mp.weixin.qq.com/s/cTRxXwWNEeb4-XX_t4bRcg   讨论模块结构信息的来源及使用方式.     ​​   1. diplomacy   diplom ...

  10. Java实现 LeetCode 299 猜数字游戏

    299. 猜数字游戏 你正在和你的朋友玩 猜数字(Bulls and Cows)游戏:你写下一个数字让你的朋友猜.每次他猜测后,你给他一个提示,告诉他有多少位数字和确切位置都猜对了(称为"B ...