类似接口,但和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());
}

最新文章

  1. win10用户文件夹重命名,启用administrator账户,删除文件夹时提示找不到该项目
  2. MSM8909平台 LED背光的控制
  3. cell1这个字符串如何截取掉前边的cell剩下后边的数字 后边数字长度不固定
  4. 再谈Cookies欺骗
  5. [转载] 与WIN不同,linux替换文件夹会删除原文件夹下的全部内容!
  6. (五)JS学习笔记 - JQuery缓存机制
  7. IOS中对于一些控件的抖动效果
  8. Codeforces 148D Bag of mice 概率dp(水
  9. windows下vue+webpack前端开发环境搭建及nginx部署
  10. 【Pattern】-NO.150.Pattern.1 -【Pattern UML】
  11. 浅谈Linux文件与目录权限
  12. input date 赋值的坑及改变时如何获取 input date的值
  13. DevExpress前端组件库
  14. 测试浏览器对HTML5标签的支持
  15. 尚硅谷springboot学习20-web开发简介
  16. springboot 集成elasticsearch5.4.3
  17. 构建基于TCP的应用层通信模型
  18. 数组合并去重Array.from
  19. jsp04状态管理
  20. spring cloud 之 Feign的使用

热门文章

  1. 2015年蓝桥杯B组C/C++决赛题解
  2. Linux学习笔记-第13天 最近有点跟不上节奏阿
  3. ESA2GJK1DH1K升级篇: 阿里云物联网平台 OTA: 关于阿里云物联网平台 OTA 的升级流程
  4. C getchar()
  5. [Taro] taro中定义以及使用全局变量
  6. Exception "java.lang.ClassNotFoundException: com/intellij/codeInsight/editorActions/FoldingData"while constructing DataFlavor for: application/x-java-jvm-local-objectref; class=com.intellij.codeInsigh
  7. Linux上error while loading shared libraries问题解决方法
  8. Redis缓存穿透,缓存击穿,缓存雪崩,热点Key
  9. Kubernetes Deployment(部署无状态应用)
  10. WPF-控件模板