Improved Exaple Error Handling: Replaced all calls to unwrap() in examples with either the ? operator or expect() #839
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.
Part of #836
This PR modifies all the examples in the
examples/
folder to use more idiomatic Rust error handling code. This PR is already quite big with quite a few (hopefully small and easy to review) changes, so converting the examples in the docs will be a separate PR later.I manually went through and ran
cargo run
for each example, so hopefully they all compile and CI will pass.Everything should still work exactly the same. This PR isn't meant to change any functionality. Just updating to the latest idioms.
Some notes:
unwrap()
are completely gone from all examples?
operator where possibleexpect()
was used to keep this PR easy to merge (should be a future discussion?)expect()
was also added in some places where it is totally fine. For example, some of the previous calls tounwrap()
were for cases we expect to be unreachable. In these casesexpect()
is just in case something goes really wrong. (no need for future discussion)unwrap()
, I also looked forpanic!()
calls and matching onResult
types to find a bunch of places where code could either be improved or completely replaced with the?
operatorTo make review as easy as possible, I avoided changing the structure of code as much as I could. That being said, I definitely noticed a lot of room for improvement in these examples. There is code that is starting to show its age and could be improved to be shorter, more idomatic, etc.
It's great to be able to give back to such a great library and I hope I'm able to contribute more in the future! Thanks for all your work on this! 😄