-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rework the error handling exercise to be based on the expression evaluator exercise #2521
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
94dc499
Rework the error handling exercise to be based on the expression eval…
randomPoison fe6e975
Add panic to exercise starting code
randomPoison 575a4b3
Reduce the time estimate for result exercise
randomPoison 8e1b976
Add speaker note
randomPoison File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,19 @@ | ||
--- | ||
minutes: 30 | ||
minutes: 20 | ||
--- | ||
|
||
# Exercise: Rewriting with Result | ||
|
||
The following implements a very simple parser for an expression language. | ||
However, it handles errors by panicking. Rewrite it to instead use idiomatic | ||
error handling and propagate errors to a return from `main`. Feel free to use | ||
[`thiserror`] and [`anyhow`]. | ||
|
||
[`thiserror`]: https://docs.rs/thiserror | ||
[`anyhow`]: https://docs.rs/anyhow | ||
|
||
> **Hint:** start by fixing error handling in the `parse` function. Once that is | ||
> working correctly, update `Tokenizer` to implement | ||
> `Iterator<Item=Result<Token, TokenizerError>>` and handle that in the parser. | ||
In this exercise we're revisiting the expression evaluator exercise that we did | ||
in day 2. Our initial solution ignores a possible error case: Dividing by zero! | ||
Rewrite `eval` to instead use idiomatic error handling to handle this error case | ||
and return an error when it occurs. We provide a simple `DivideByZeroError` type | ||
to use as the error type for `eval`. | ||
|
||
```rust,editable | ||
{{#include exercise.rs:types}} | ||
|
||
{{#include exercise.rs:panics}} | ||
{{#include exercise.rs:eval}} | ||
|
||
{{#include exercise.rs:tests}} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
# Solution | ||
|
||
<!-- compile_fail because `mdbook test` does not allow use of `thiserror` --> | ||
```rust,editable | ||
{{#include exercise.rs:types}} | ||
|
||
```rust,editable,compile_fail | ||
{{#include exercise.rs:solution}} | ||
|
||
{{#include exercise.rs:tests}} | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/pattern-matching/exercise.rs
does not contain this conditional (it just relies on/
to panic).I think that's OK, but maybe claiming it's "the original" in the comment above is inaccurate enough to bother folks (like me)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah true it's not exactly the same as the solution to the earlier exercise, I deliberately added in the panic to show students where exactly the error case is. Personally I don't think that'd be too confusing for students, but let me add a speaker note calling that out to teachers so they can mention it if students are confused.