Rust中的Trait
2024-08-25 20:01:39
类似接口,但和php中的trait又有点不一样。
pub trait Summary { fn summarize(&self) -> String; } pub struct NewArticle { pub headline: String, pub author: String, } pub struct Tweet { pub username: String, pub content: String, pub reply: bool, pub retweet: bool, } impl Summary for Tweet { fn summarize(&self) -> String { format!("{}: {}", self.username, self.content) } } impl Summary for NewArticle { fn summarize(&self) -> String { format!("{}, by {}", self.headline, self.author) } } fn main() { let tweet = Tweet { username: String::from("horse_ebooks"), content: String::from("of course, as you problem already know, people"), reply: false, retweet: false, }; println!("1 new tweet: {}", tweet.summarize()); let article = NewArticle { headline: String::from("all will be ok"), author: String::from("sky chan"), }; println!("1 new article: {}", article.summarize()); }
最新文章
- win10用户文件夹重命名,启用administrator账户,删除文件夹时提示找不到该项目
- MSM8909平台 LED背光的控制
- cell1这个字符串如何截取掉前边的cell剩下后边的数字 后边数字长度不固定
- 再谈Cookies欺骗
- [转载] 与WIN不同,linux替换文件夹会删除原文件夹下的全部内容!
- (五)JS学习笔记 - JQuery缓存机制
- IOS中对于一些控件的抖动效果
- Codeforces 148D Bag of mice 概率dp(水
- windows下vue+webpack前端开发环境搭建及nginx部署
- 【Pattern】-NO.150.Pattern.1 -【Pattern UML】
- 浅谈Linux文件与目录权限
- input date 赋值的坑及改变时如何获取 input date的值
- DevExpress前端组件库
- 测试浏览器对HTML5标签的支持
- 尚硅谷springboot学习20-web开发简介
- springboot 集成elasticsearch5.4.3
- 构建基于TCP的应用层通信模型
- 数组合并去重Array.from
- jsp04状态管理
- spring cloud 之 Feign的使用
热门文章
- 2015年蓝桥杯B组C/C++决赛题解
- Linux学习笔记-第13天 最近有点跟不上节奏阿
- ESA2GJK1DH1K升级篇: 阿里云物联网平台 OTA: 关于阿里云物联网平台 OTA 的升级流程
- C getchar()
- [Taro] taro中定义以及使用全局变量
- Exception ";java.lang.ClassNotFoundException: com/intellij/codeInsight/editorActions/FoldingData";while constructing DataFlavor for: application/x-java-jvm-local-objectref; class=com.intellij.codeInsigh
- Linux上error while loading shared libraries问题解决方法
- Redis缓存穿透,缓存击穿,缓存雪崩,热点Key
- Kubernetes Deployment(部署无状态应用)
- WPF-控件模板