fust_tutorials/
lib.rs

1// mod front_of_house 后使用分号,而不是代码块
2// 这将告诉 Rust 在另一个与模块同名的文件中加载模块的内容
3mod front_of_house;
4
5pub use front_of_house::hosting;
6
7
8pub fn eat_at_restaurant() {
9    hosting::add_to_waitlist();
10}
11
12
13// 定义一组行为
14pub trait Summary {
15    fn summarize_author(&self) -> String;
16
17    // 默认行为, 可以重写
18    fn summarize(&self) -> String {
19        // 因为不知道谁实现了 trait, 所以不能用 self 的成员
20        format!("Read more from {}...", self.summarize_author())
21    }
22}
23
24struct News {
25    pub headline: String,
26    pub location: String,
27    pub author: String,
28    pub content: String,
29}
30
31pub struct Tweet {
32    pub author: String,
33    pub content: String,
34    pub reply_number: usize,
35    pub can_retweet: bool,
36}
37
38
39impl Summary for News {
40    fn summarize_author(&self) -> String {
41        self.author.to_string()
42    }
43
44    // 重写默认实现
45    fn summarize(&self) -> String {
46        format!("{}, by {} ({})", self.headline, self.author, self.location)
47    }
48}
49
50impl Summary for Tweet {
51    fn summarize_author(&self) -> String {
52        self.author.to_string()
53    }
54}
55
56pub struct Rectangle {
57    pub width: u32,
58    pub height: u32
59}
60
61impl Rectangle {
62    // 可简写为 &self
63    pub fn area(self: &Self) -> u32 {
64        self.height * self.height
65    }
66
67    pub fn square(size: u32) -> Self {
68        Self { width: size, height: size }
69    }
70
71    fn can_hold(&self, other: &Self) -> bool {
72        self.width > other.width && self.height > other.height
73    }
74}
75
76mod guess;
77
78#[cfg(test)]
79mod tests {
80    #[test]
81    fn test_add() {
82        assert_eq!(2 + 2, 4);
83    }
84
85    use super::*;
86
87    #[test]
88    fn larger_can_hold_smaller() {
89        let larger = Rectangle {
90            width: 8,
91            height: 7,
92        };
93
94        let smaller = Rectangle {
95            width: 5, 
96            height: 1,
97        };
98
99        assert!(larger.can_hold(&smaller));
100    }
101
102    fn greeting(name: &str) -> String {
103        format!("heelo,{}", name)
104    }
105
106    #[test]
107    fn contain_name() {
108        let result = greeting("dawson");
109
110        assert!(
111            result.contains("dawson"),
112            "not contain nmae, value was {}",
113            result
114        );
115    }
116
117    use guess::Guess;
118
119    #[test]
120    // #[should_panic]    // 都可以
121    #[should_panic(expected = "Guess value must be between 1 - 100")]   // 信息是 panic 的字串
122    fn greter_than_100() {
123        Guess::new(200);
124
125    }
126
127    #[test]
128    #[ignore]
129    fn it_works() -> Result<(), String> {
130        if 2 + 2 == 4 {
131            Ok(())
132        } else {
133            Err(String::from("2 + 2 != 4"))
134        }
135    }
136}
137
138/// Add two to the number given.
139/// 
140/// # Examples
141/// ```
142/// let arg = 5;
143/// let answer = fust_tutorials::add_two(arg);
144/// 
145/// assert_eq!(7, answer);
146/// 
147pub fn add_two(num: i32) -> i32 {
148    num + 2
149}