-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Guide: explains the enum/match relationship #18497
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
Conversation
GoodGuy(string), | ||
BadGuy(string), | ||
Peon, | ||
} |
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.
Lacks closing backticks.
Thanks for taking some initiative on this! |
and can be even more useful when they're generic across types. But before we get to | ||
generics, let's talk about how to use them with pattern matching, a tool that will | ||
let us deconstruct this sum types in a very elegant way (a classic in funtional | ||
programing) and avoid all these messy `if/else` |
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.
s/programing/programming/
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.
// Add `s.`
s/`if/else`/`if/else`s./`
Updated :) What do you think @steveklabnik ? |
} | ||
``` | ||
|
||
And that is how you handle enums, and thus can use the value contained in the. |
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.
contained in the.
... is followed by?
@mdinger corrected :) (damn you spaces before punctuation, you are the exact opposite in French...) |
``` | ||
Where a `Result` is either an `OK`, with a result of a computation, or an `ErrorReason` | ||
with a `String` explaining what caused the computation to fail. This enum is actually | ||
very useful as you can guess and is even a part of the standard library. |
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.
I like this idea, but naming it Result
introduces shadowing. This is why I made the previous one OptionalInt
. Can we maybe do something similar here?
Also the 'as you can guess' makes this flow a little awkward to me.
I do think this is a significnatly better example, thank you :)
This just keeps getting better. I have a few more things, but they're all very small, and I'm really happy with how this is turning out. |
@steveklabnik edited :) |
Wonderful, thank you so much! |
Closes #18169