1mod 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
13pub trait Summary {
15 fn summarize_author(&self) -> String;
16
17 fn summarize(&self) -> String {
19 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 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 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(expected = "Guess value must be between 1 - 100")] 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
138pub fn add_two(num: i32) -> i32 {
148 num + 2
149}