rust_study/
lib.rs

1//! # My Crate
2//!
3//! `my_crate` is a collection of utilities to make performing certain
4//! calculations more convenient.
5
6//定义 trait
7pub trait Summary {
8  // 以下方法 实现 size 具体逻辑
9  fn size(&self) -> String;
10  
11  // 默认字符串
12  fn normal(&self) -> String {
13    String::from("(Read more...)")
14  }
15  
16  fn summary_author(&self) -> String;
17
18  fn summary_fn(&self) -> String {
19    format!("(Read more form {} ...)", self.summary_author())
20  }
21}
22
23pub struct NewsArticle {
24  pub headline: String,
25  pub localhost: String,
26  pub author: String,
27  pub content:String,
28}
29
30impl Summary for NewsArticle {
31    fn size(&self) -> String {
32      format!("{}, by {} {}", self.headline, self.author, self.localhost)
33    }
34    fn summary_author(&self) -> String {
35      format!("{}", self.author)
36    }
37}
38
39pub struct Tweet {
40  pub username: String,
41  pub content: String,
42  pub reply:bool,
43  pub retweet:bool,
44}
45
46impl Summary for Tweet {
47  fn size(&self) -> String {
48    format!("{}:{}", self.username, self.content)
49  }
50
51  fn summary_author(&self) -> String {
52    format!("{}", self.username)
53  }
54}
55
56/// adds one to the number given
57/// 
58/// # Examples
59/// 
60/// ```
61/// let arg = 5;
62/// let answer = rust_study::add_one(arg);
63/// 
64/// assert_eq!(6, answer);
65/// ```
66pub fn add_one(x: i32) -> i32 {
67  x + 1
68}