Skip to content

Commit 9bdb0a1

Browse files
author
marisa
committed
feat: Refactor hint system
Hints are now accessible using the CLI subcommand `rustlings hint <exercise name`. BREAKING CHANGE: This fundamentally changes the way people interact with exercises.
1 parent 627cdc0 commit 9bdb0a1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+400
-1681
lines changed

exercises/enums/enums1.rs

+1-29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// enums1.rs
2-
// Make me compile! Scroll down for hints!
2+
// Make me compile! Execute `rustlings hint enums1` for hints!
33

44
#[derive(Debug)]
55
enum Message {
@@ -12,31 +12,3 @@ fn main() {
1212
println!("{:?}", Message::Move);
1313
println!("{:?}", Message::ChangeColor);
1414
}
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-
// Hint: The declaration of the enumeration type has not been defined yet.

exercises/enums/enums2.rs

+1-36
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// enums2.rs
2-
// Make me compile! Scroll down for hints
2+
// Make me compile! Execute `rustlings hint enums2` for hints!
33

44
#[derive(Debug)]
55
enum Message {
@@ -24,38 +24,3 @@ fn main() {
2424
message.call();
2525
}
2626
}
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-
// Hint: you can create enumerations that have different variants with different types
61-
// such as no data, anonymous structs, a single string, tuples, ...etc

exercises/error_handling/errors1.rs

+1-34
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// was, instead of just sometimes returning `None`. The 2nd test currently
55
// does not compile or pass, but it illustrates the behavior we would like
66
// this function to have.
7-
// Scroll down for hints!!!
7+
// Execute `rustlings hint errors1` for hints!
88

99
pub fn generate_nametag_text(name: String) -> Option<String> {
1010
if name.len() > 0 {
@@ -38,36 +38,3 @@ mod tests {
3838
);
3939
}
4040
}
41-
42-
43-
44-
45-
46-
47-
48-
49-
50-
51-
52-
53-
54-
55-
56-
57-
58-
59-
60-
61-
// `Err` is one of the variants of `Result`, so what the 2nd test is saying
62-
// is that `generate_nametag_text` should return a `Result` instead of an
63-
// `Option`.
64-
65-
// To make this change, you'll need to:
66-
// - update the return type in the function signature to be a Result<String, String> that
67-
// could be the variants `Ok(String)` and `Err(String)`
68-
// - change the body of the function to return `Ok(stuff)` where it currently
69-
// returns `Some(stuff)`
70-
// - change the body of the function to return `Err(error message)` where it
71-
// currently returns `None`
72-
// - change the first test to expect `Ok(stuff)` where it currently expects
73-
// `Some(stuff)`.

exercises/error_handling/errors2.rs

+1-25
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// and add.
1515

1616
// There are at least two ways to implement this that are both correct-- but
17-
// one is a lot shorter! Scroll down for hints to both ways.
17+
// one is a lot shorter! Execute `rustlings hint errors2` for hints to both ways.
1818

1919
use std::num::ParseIntError;
2020

@@ -43,27 +43,3 @@ mod tests {
4343
);
4444
}
4545
}
46-
47-
48-
49-
50-
51-
52-
53-
54-
55-
56-
57-
58-
59-
60-
61-
62-
63-
// One way to handle this is using a `match` statement on
64-
// `item_quantity.parse::<i32>()` where the cases are `Ok(something)` and
65-
// `Err(something)`. This pattern is very common in Rust, though, so there's
66-
// a `?` operator that does pretty much what you would make that match statement
67-
// do for you! Take a look at this section of the Error Handling chapter:
68-
// https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-the--operator
69-
// and give it a try!

exercises/error_handling/errors3.rs

+2-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// errors3.rs
22
// This is a program that is trying to use a completed version of the
33
// `total_cost` function from the previous exercise. It's not working though!
4-
// Why not? What should we do to fix it? Scroll for hints!
4+
// Why not? What should we do to fix it?
5+
// Execute `rustlings hint errors3` for hints!
56

67
use std::num::ParseIntError;
78

@@ -26,22 +27,3 @@ pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
2627

2728
Ok(qty * cost_per_item + processing_fee)
2829
}
29-
30-
31-
32-
33-
34-
35-
36-
37-
38-
39-
40-
41-
42-
43-
44-
45-
46-
47-
// If other functions can return a `Result`, why shouldn't `main`?

exercises/error_handling/errorsn.rs

+1-136
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// type goes where the question marks are, and how do we return
1414
// that type from the body of read_and_validate?
1515
//
16-
// Scroll down for hints :)
16+
// Execute `rustlings hint errors4` for hints :)
1717

1818
use std::error;
1919
use std::fmt;
@@ -110,138 +110,3 @@ impl error::Error for CreationError {
110110
}
111111
}
112112
}
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-
// First hint: To figure out what type should go where the ??? is, take a look
145-
// at the test helper function `test_with_str`, since it returns whatever
146-
// `read_and_validate` returns and`test_with_str` has its signature fully
147-
// specified.
148-
149-
150-
151-
152-
153-
154-
155-
156-
157-
158-
159-
160-
161-
162-
163-
164-
165-
166-
167-
168-
// Next hint: There are three places in `read_and_validate` that we call a
169-
// function that returns a `Result` (that is, the functions might fail).
170-
// Apply the `?` operator on those calls so that we return immediately from
171-
// `read_and_validate` if those function calls fail.
172-
173-
174-
175-
176-
177-
178-
179-
180-
181-
182-
183-
184-
185-
186-
187-
188-
189-
190-
191-
192-
// Another hint: under the hood, the `?` operator calls `From::from`
193-
// on the error value to convert it to a boxed trait object, a Box<dyn error::Error>,
194-
// which is polymorphic-- that means that lots of different kinds of errors
195-
// can be returned from the same function because all errors act the same
196-
// since they all implement the `error::Error` trait.
197-
// Check out this section of the book:
198-
// https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-the--operator
199-
200-
201-
202-
203-
204-
205-
206-
207-
208-
209-
210-
211-
212-
213-
214-
215-
216-
217-
218-
219-
// Another another hint: Note that because the `?` operator returns
220-
// the *unwrapped* value in the `Ok` case, if we want to return a `Result` from
221-
// `read_and_validate` for *its* success case, we'll have to rewrap a value
222-
// that we got from the return value of a `?`ed call in an `Ok`-- this will
223-
// look like `Ok(something)`.
224-
225-
226-
227-
228-
229-
230-
231-
232-
233-
234-
235-
236-
237-
238-
239-
240-
241-
242-
243-
244-
// Another another another hint: `Result`s must be "used", that is, you'll
245-
// get a warning if you don't handle a `Result` that you get in your
246-
// function. Read more about that in the `std::result` module docs:
247-
// https://doc.rust-lang.org/std/result/#results-must-be-used

exercises/error_handling/option1.rs

+1-29
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// This example panics because the second time it calls `pop`, the `vec`
33
// is empty, so `pop` returns `None`, and `unwrap` panics if it's called
44
// on `None`. Handle this in a more graceful way than calling `unwrap`!
5-
// Scroll down for hints :)
5+
// Execute `rustlings hint option1` for hints :)
66

77
pub fn pop_too_much() -> bool {
88
let mut list = vec![3];
@@ -27,31 +27,3 @@ mod tests {
2727
assert!(pop_too_much());
2828
}
2929
}
30-
31-
32-
33-
34-
35-
36-
37-
38-
39-
40-
41-
42-
43-
44-
45-
46-
47-
48-
49-
50-
51-
52-
53-
// Try using a `match` statement where the arms are `Some(thing)` and `None`.
54-
// Or set a default value to print out if you get `None` by using the
55-
// function `unwrap_or`.
56-
// Or use an `if let` statement on the result of `pop()` to both destructure
57-
// a `Some` value and only print out something if we have a value!

0 commit comments

Comments
 (0)