Simplifying matching routes and constructing responses #6
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.
Hi,
I'm reading your book, I really like it!
I'm a rust beginner. While coding along at home and digging into what means what, I stumbled upon some possible code tweakings for chapter 3, which I'm sending below in case they might interrest you.
matching on
Method
enums instead of stringSimilarly, using
StatusCode
enum instead of parsing integers, which makes the code more readable and avoids one call to.unwrap()
Maybe it's just my personnal preference, I find the
if let ... =
matching syntax forOption
more readableOther opinionated change: I let the
generate_api_response()
take full ownership of theBody
instead of passing it as reference, since it's normally meant to be called last in the code flow, it avoidsclone()
ing the response to the POST method right after creating it, and it's inline with how theResponse::builder()
is structured.One last aspect that was puzzling me was that sometimes the code is returning the output of
generate_api_response(()
directly, whereas sometimes it's unrapping it with?
then immediately re-bundling it into anotherOk
. Maybe there's some error we want to let bubble up in some cases that I don't understand, so I didn't dare to touch that part 😄I hope you find some of this useful, let me know your thoughts!