@@ -18,141 +18,71 @@ Run the test suite. It can be run with `cargo`, which is installed with rust.
18
18
$ cargo test
19
19
```
20
20
21
- This will fail, complaining that ` hello-world ` could not compile .
21
+ This will compile the library and run the test, which fails .
22
22
23
- To fix this, create a new directory called ` src ` .
24
- Create a new file called, ` lib.rs ` , inside the ` src ` directory.
25
-
26
- ## Step 2
27
-
28
- Run the test again. It will give you a new error, another compile error.
29
- Our ` lib.rs ` does not contain any code, specifically the ` hello() `
30
- function that our test is looking for.
31
-
32
- ### Fixing the Error
33
-
34
- To fix it, open up the ` src/lib.rs ` file and add the following code:
35
-
36
- ``` rust
37
- pub fn hello (name : Option <& str >) -> String {
38
- "" . to_string ()
39
- }
40
23
```
41
-
42
- Our test is looking for the ` hello() ` function from the ` hello_world `
43
- crate. ` lib.rs ` , by default, is our crate root and our test
44
- is looking for the ` hello() ` function there.
45
-
46
- The code we are adding to ` lib.rs ` defines a public function (` pub fn ` ) that is called "hello".
47
- The function accepts a ` name ` as an optional argument (` Option ` ).
48
- The function returns a ` String ` .
49
- We start by returning an empty string (` "".to_string() ` ).
50
-
51
- ## Step 3
52
-
53
- Run the test again.
54
-
55
- This time, code compilation will pass and we receive actual test failures.
56
-
57
- ```
58
- running 3 tests
59
- test test_other_same_name ... ignored
60
- test test_sample_name ... ignored
61
- test test_no_name ... FAILED
24
+ running 1 test
25
+ test test_hello_world ... FAILED
62
26
63
27
failures:
64
28
65
- ---- test_no_name stdout ----
66
- thread 'test_no_name' panicked at 'assertion failed: `(left == right)`
67
- (left: `"Hello, World!"`, right: `""`)', tests/hello-world.rs:5
68
-
29
+ ---- test_hello_world stdout ----
30
+ thread 'test_hello_world' panicked at 'assertion failed: `(left == right)`
31
+ (left: `"Hello, World!"`, right: `"Goodbye, World!"`)', tests/hello-world.rs:5
69
32
70
33
failures:
71
- test_no_name
34
+ test_hello_world
72
35
73
- test result: FAILED. 0 passed; 1 failed; 2 ignored; 0 measured
36
+ test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured
74
37
```
75
38
76
39
### Understanding Test Failures
77
40
78
- Only one of the tests runs (` test_no_name ` ) and it fails. The other
79
- tests are ignored (more on that later).
80
-
81
- The ` test_no_name ` failure states that it is expecting the value,
82
- ` "Hello, World!" ` , to be returned from ` hello("") ` .
41
+ The ` test_hello_world ` failure states that it is expecting the value,
42
+ ` "Hello, World!" ` , to be returned from ` hello() ` .
83
43
The left side of the assertion (at line 5) should be equal to the right side.
84
44
85
45
```
86
- ---- test_no_name stdout ----
87
- thread 'test_no_name ' panicked at 'assertion failed: `(left == right)`
88
- (left: `"Hello, World!"`, right: `""`)', tests/hello-world.rs:5
46
+ ---- test_hello_world stdout ----
47
+ thread 'test_hello_world ' panicked at 'assertion failed: `(left == right)`
48
+ (left: `"Hello, World!"`, right: `"Goodbye, World! "`)', tests/hello-world.rs:5
89
49
```
90
50
91
- To fix it, let's return ` "Hello, World!" ` , instead of an empty string
92
- ( ` "" ` ) inside our ` hello ` function .
51
+ To fix it, open up ` src/lib.rs ` and change the ` hello ` function to return
52
+ ` "Hello, World!" ` instead of "Goodbye, World!" .
93
53
94
54
``` rust
95
- pub fn hello (name : Option < & str > ) -> String {
96
- " Hello, World!" . to_string ()
55
+ pub fn hello () -> & ' static str {
56
+ " Hello, World!"
97
57
}
98
58
```
99
59
100
- ## Step 4
60
+ ## Step 2
101
61
102
62
Run the test again. This time, it will pass.
103
63
104
64
```
105
- running 3 tests
106
- test test_other_same_name ... ignored
107
- test test_sample_name ... ignored
108
- test test_no_name ... ok
109
-
110
- test result: ok. 1 passed; 0 failed; 2 ignored; 0 measured
111
-
112
- Doc-tests hello-world
113
-
114
65
running 0 tests
115
66
116
67
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
117
- ```
118
-
119
- You may have noticed compiler warnings earlier:
120
-
121
- ```
122
- Compiling hello-world v0.0.0
123
- (file:////exercism/exercises/rust/hello-world)
124
- src/lib.rs:1:14: 1:18 warning: unused variable: `name`, #[warn(unused_variables)] on by default
125
- src/lib.rs:1 pub fn hello(name: Option<&str>) -> String {
126
- ^~~~
127
- ```
128
-
129
- Our ` hello ` function does not use the ` name ` argument so the
130
- compiler is letting us know that we could potentially remove the
131
- argument from our function (it likes "clean code").
132
-
133
- As we make the rest of the tests pass, we will find that we need the ` name `
134
- argument, so don't delete it.
135
68
136
- Activate the next test. Open the ` tests/hello-world.rs ` file.
137
- Delete the ` #[ignore] ` line for the ` test_sample_name ` test.
69
+ Running target/debug/deps/hello_world-bd1f06dc726ef14f
138
70
139
- ## Step 5
71
+ running 1 test
72
+ test test_hello_world ... ok
140
73
141
- Run the test suite again. Read the test failure and make the test pass.
74
+ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
142
75
143
- As a reminder, the [ rust book] ( http://doc.rust-lang.org/stable/book/README.html )
144
- is a good reference for understanding rust.
145
- The cargo output may also have hints to help you, depending on the errors you get.
146
- For example, ` rustc --explain E0425 ` will explain unresolved name errors.
76
+ Doc-tests hello-world
147
77
148
- ## Wash, Rinse, Repeat
78
+ running 0 tests
149
79
150
- Delete one ` #[ignore] ` at a time, and make each test pass before you move to
151
- the next one.
80
+ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
81
+ ```
152
82
153
83
## Submit
154
84
155
- When everything is passing, you can submit your code with the following
85
+ Once the test is passing, you can submit your code with the following
156
86
command:
157
87
158
88
```
0 commit comments