1#[derive(Debug)]
2struct Rectangle {
3 width: u32,
4 height: u32,
5}
6
7impl Rectangle {
8 fn can_hold(&self, other: &Rectangle) -> bool {
9 self.width > other.width && self.height > other.height
10 }
11}
12pub fn add_two(a: i32) -> i32 {
23 a + 2
24}
25
26pub struct Guess {
27 value: i32,
28}
29
30impl Guess {
31 pub fn new(value: i32) -> Guess {
32 if value < 1 {
33 panic!(
34 "Guess value must be greater than or equal to 1, got {}.",
35 value
36 );
37 } else if value > 100 {
38 panic!(
39 "Guess value must be less than or equal to 100, got {}.",
40 value
41 );
42 }
43
44 Guess { value }
45 }
46}
47#[cfg(test)]
48mod tests {
49 use super::*;
50
51 #[test]
52 fn it_works() {
53 assert_eq!(2 + 2, 4);
54 }
55 #[test]
56 #[ignore]
57 fn another() {
58 panic!("Make this test fail");
59 }
60
61 #[test]
62 fn larger_can_hold_smaller() {
63 let larger = Rectangle {
64 width: 8,
65 height: 7,
66 };
67 let smaller = Rectangle {
68 width: 5,
69 height: 1,
70 };
71
72 assert!(larger.can_hold(&smaller));
73 }
74 #[test]
75 fn smaller_cannot_hold_larger() {
76 let larger = Rectangle {
77 width: 8,
78 height: 7,
79 };
80 let smaller = Rectangle {
81 width: 5,
82 height: 1,
83 };
84
85 assert!(!smaller.can_hold(&larger));
86 }
87
88 #[test]
89 fn id_adds_two() {
90 assert_eq!(4, add_two(2));
91 }
92
93 #[test]
94 #[should_panic(expected = "Guess value must be less than or equal to 100")]
95 fn greater_than_100() {
96 Guess::new(101);
97 }
98
99 #[test]
100 fn it_works_result() -> Result<(), String> {
101 if 2 + 2 == 4 {
102 Ok(())
103 } else {
104 Err(String::from("two plus two does not equal four"))
105 }
106 }
107}