forked from rust-lang/rustlings
-
Notifications
You must be signed in to change notification settings - Fork 1
/
part2.toml
289 lines (236 loc) · 9.25 KB
/
part2.toml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
[[exercises]]
name = "functions1"
path = "exercises/02_functions/functions1.rs"
mode = "compile"
hint = """
This main function is calling a function that it expects to exist, but the
function doesn't exist. It expects this function to have the name `call_me`.
It expects this function to not take any arguments and not return a value.
Sounds a lot like `main`, doesn't it?"""
[[exercises]]
name = "functions2"
path = "exercises/02_functions/functions2.rs"
mode = "compile"
hint = """
Rust requires that all parts of a function's signature have type annotations,
but `call_me` is missing the type annotation of `num`."""
[[exercises]]
name = "functions3"
path = "exercises/02_functions/functions3.rs"
mode = "compile"
hint = """
This time, the function *declaration* is okay, but there's something wrong
with the place where we're calling the function.
As a reminder, you can freely play around with different solutions in Rustlings!
Watch mode will only jump to the next exercise if you remove the `I AM NOT
DONE` comment."""
[[exercises]]
name = "functions4"
path = "exercises/02_functions/functions4.rs"
mode = "compile"
hint = """
The error message points to the function `sale_price` and says it expects a type
after the `->`. This is where the function's return type should be -- take a
look at the `is_even` function for an example!
Also: Did you figure out that, technically, `u32` would be the more fitting type
for the prices here, since they can't be negative? If so, kudos!"""
[[exercises]]
name = "functions5"
path = "exercises/02_functions/functions5.rs"
mode = "compile"
hint = """
This is a really common error that can be fixed by removing one character.
It happens because Rust distinguishes between expressions and statements:
expressions return a value based on their operand(s), and statements simply
return a `()` type which behaves just like `void` in C/C++ language.
We want to return a value of `i32` type from the `square` function, but it is
returning a `()` type...
They are not the same. There are two solutions:
1. Add a `return` ahead of `num * num;`
2. remove `;`, make it to be `num * num`"""
# IF
[[exercises]]
name = "if1"
path = "exercises/03_if/if1.rs"
mode = "test"
hint = """
It's possible to do this in one line if you would like!
Some similar examples from other languages:
- In C(++) this would be: `a > b ? a : b`
- In Python this would be: `a if a > b else b`
Remember in Rust that:
- the `if` condition does not need to be surrounded by parentheses
- `if`/`else` conditionals are expressions
- Each condition is followed by a `{}` block."""
[[exercises]]
name = "if2"
path = "exercises/03_if/if2.rs"
mode = "test"
hint = """
For that first compiler error, it's important in Rust that each conditional
block returns the same type! To get the tests passing, you will need a couple
conditions checking different input values."""
[[exercises]]
name = "if3"
path = "exercises/03_if/if3.rs"
mode = "test"
hint = """
In Rust, every arm of an `if` expression has to return the same type of value.
Make sure the type is consistent across all arms."""
# QUIZ 1
[[exercises]]
name = "quiz1"
path = "exercises/quiz1.rs"
mode = "test"
hint = "No hints this time ;)"
[[exercises]]
name = "structs1"
path = "exercises/07_structs/structs1.rs"
mode = "test"
hint = """
Rust has more than one type of struct. Three actually, all variants are used to
package related data together.
There are normal (or classic) structs. These are named collections of related
data stored in fields.
Tuple structs are basically just named tuples.
Finally, Unit-like structs. These don't have any fields and are useful for
generics.
In this exercise you need to complete and implement one of each kind.
Read more about structs in The Book:
https://doc.rust-lang.org/book/ch05-01-defining-structs.html"""
[[exercises]]
name = "structs2"
path = "exercises/07_structs/structs2.rs"
mode = "test"
hint = """
Creating instances of structs is easy, all you need to do is assign some values
to its fields.
There are however some shortcuts that can be taken when instantiating structs.
Have a look in The Book, to find out more:
https://doc.rust-lang.org/stable/book/ch05-01-defining-structs.html#creating-instances-from-other-instances-with-struct-update-syntax"""
[[exercises]]
name = "structs3"
path = "exercises/07_structs/structs3.rs"
mode = "test"
hint = """
For `is_international`: What makes a package international? Seems related to
the places it goes through right?
For `get_fees`: This method takes an additional argument, is there a field in
the `Package` struct that this relates to?
Have a look in The Book, to find out more about method implementations:
https://doc.rust-lang.org/book/ch05-03-method-syntax.html"""
[[exercises]]
name = "enums1"
path = "exercises/08_enums/enums1.rs"
mode = "compile"
hint = "No hints this time ;)"
[[exercises]]
name = "enums2"
path = "exercises/08_enums/enums2.rs"
mode = "compile"
hint = """
You can create enumerations that have different variants with different types
such as no data, anonymous structs, a single string, tuples, ...etc"""
[[exercises]]
name = "enums3"
path = "exercises/08_enums/enums3.rs"
mode = "test"
hint = """
As a first step, you can define enums to compile this code without errors.
And then create a match expression in `process()`.
Note that you need to deconstruct some message variants in the match expression
to get value in the variant."""
[[exercises]]
name = "options1"
path = "exercises/12_options/options1.rs"
mode = "test"
hint = """
Options can have a `Some` value, with an inner value, or a `None` value,
without an inner value.
There's multiple ways to get at the inner value, you can use `unwrap`, or
pattern match. Unwrapping is the easiest, but how do you do it safely so that
it doesn't panic in your face later?"""
[[exercises]]
name = "options2"
path = "exercises/12_options/options2.rs"
mode = "test"
hint = """
Check out:
- https://doc.rust-lang.org/rust-by-example/flow_control/if_let.html
- https://doc.rust-lang.org/rust-by-example/flow_control/while_let.html
Remember that `Option`s can be stacked in `if let` and `while let`.
For example: `Some(Some(variable)) = variable2`
Also see `Option::flatten`
"""
[[exercises]]
name = "options3"
path = "exercises/12_options/options3.rs"
mode = "compile"
hint = """
The compiler says a partial move happened in the `match` statement. How can
this be avoided? The compiler shows the correction needed.
After making the correction as suggested by the compiler, do read:
https://doc.rust-lang.org/std/keyword.ref.html"""
[[exercises]]
name = "vecs1"
path = "exercises/05_vecs/vecs1.rs"
mode = "test"
hint = """
In Rust, there are two ways to define a Vector.
1. One way is to use the `Vec::new()` function to create a new vector
and fill it with the `push()` method.
2. The second way, which is simpler is to use the `vec![]` macro and
define your elements inside the square brackets.
Check this chapter: https://doc.rust-lang.org/stable/book/ch08-01-vectors.html
of the Rust book to learn more.
"""
[[exercises]]
name = "vecs2"
path = "exercises/05_vecs/vecs2.rs"
mode = "test"
hint = """
In the first function we are looping over the Vector and getting a reference to
one `element` at a time.
To modify the value of that `element` we need to use the `*` dereference
operator. You can learn more in this chapter of the Rust book:
https://doc.rust-lang.org/stable/book/ch08-01-vectors.html#iterating-over-the-values-in-a-vector
In the second function this dereferencing is not necessary, because the `map`
function expects the new value to be returned.
After you've completed both functions, decide for yourself which approach you
like better.
What do you think is the more commonly used pattern under Rust developers?
"""
[[exercises]]
name = "strings1"
path = "exercises/09_strings/strings1.rs"
mode = "compile"
hint = """
The `current_favorite_color` function is currently returning a string slice
with the `'static` lifetime. We know this because the data of the string lives
in our code itself -- it doesn't come from a file or user input or another
program -- so it will live as long as our program lives.
But it is still a string slice. There's one way to create a `String` by
converting a string slice covered in the Strings chapter of the book, and
another way that uses the `From` trait."""
[[exercises]]
name = "strings2"
path = "exercises/09_strings/strings2.rs"
mode = "compile"
hint = """
Yes, it would be really easy to fix this by just changing the value bound to
`word` to be a string slice instead of a `String`, wouldn't it?? There is a way
to add one character to the `if` statement, though, that will coerce the
`String` into a string slice.
Side note: If you're interested in learning about how this kind of reference
conversion works, you can jump ahead in the book and read this part in the
smart pointers chapter:
https://doc.rust-lang.org/stable/book/ch15-02-deref.html#implicit-deref-coercions-with-functions-and-methods"""
[[exercises]]
name = "strings3"
path = "exercises/09_strings/strings3.rs"
mode = "test"
hint = """
There's tons of useful standard library functions for strings. Let's try and use some of them:
https://doc.rust-lang.org/std/string/struct.String.html#method.trim
For the `compose_me` method: You can either use the `format!` macro, or convert
the string slice into an owned string, which you can then freely extend."""