Skip to content

Guide: Enums chapter contains unexplained match keyword #18169

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

Closed
lgrahl opened this issue Oct 19, 2014 · 10 comments · Fixed by #18497
Closed

Guide: Enums chapter contains unexplained match keyword #18169

lgrahl opened this issue Oct 19, 2014 · 10 comments · Fixed by #18497

Comments

@lgrahl
Copy link

lgrahl commented Oct 19, 2014

See: https://github.com/rust-lang/rust/blob/master/src/doc/guide.md#enums

The match keyword hasn't been introduced until that point but it's used in the code example which is confusing.

I believe an if/else solution would be more explanatory, especially because the reader probably wants to see how to unpack the Value variant of enum OptionalInt. The same enum could then be used with the match keyword in the beginning of the next chapter (Match).

@steveklabnik
Copy link
Member

You are correct. Nice catch!

@gamazeps
Copy link
Contributor

Are you ok with simply reordering them ?
Because introducing sum type without precedent knowledge of pattern matching is a bit pointles (or at least misses the fact that they are juste AWESOME for pattern matching).
If you're ok with this I'll PR in a few hours :)

@steveklabnik
Copy link
Member

Maybe, if it flows okay, yeah. I do want to put SOME kind of mention of it first.

@lgrahl
Copy link
Author

lgrahl commented Oct 31, 2014

It's probably easy to find out what match is doing by simply looking at it. This part of the tutorial is just a bit confusing and should be fixed. However, my main concern here is that the reader is left clueless on how to solve this problem with if/else:

let x = Value(5);
let y = Missing;

match x {
    Value(n) => println!("x is {}", n),
    Missing  => println!("x is missing!"),
}

In fact, I really don't know how the equivalent code without match would look like. I've briefly tried to figure it out and gave up.

@gamazeps
Copy link
Contributor

With an Option you coul unwrap I guess.

By the way @steveklabnik why did you write a fake option enum ?
Because that's basically what is done with this, it might be better to give out the real name

@steveklabnik
Copy link
Member

Because we haven't talked about generics yet.

gamazeps added a commit to gamazeps/rust that referenced this issue Nov 4, 2014
@lgrahl
Copy link
Author

lgrahl commented Nov 5, 2014

Nicely done. However, my other concern, how this example could be done without match, remains unresolved. I'm certain that I'm not the only one who wants to know how it works with if/else before continue reading about match. Should I open another issue for that matter?

@gamazeps
Copy link
Contributor

gamazeps commented Nov 5, 2014

@lgrahl I'm honestly not sure that it is possible with an arbitrary enum...
You can test for some arbitrary values but i guess but that's it.

Bt it's more an issue of sum types in general rather than a Rust issue (I can't find ways to do that in haskell nor in scala)

@lgrahl
Copy link
Author

lgrahl commented Nov 5, 2014

@gamazeps Alright, I expected something like this as I was unable to figure out how to write it without match. I don't have much experience with statically typed languages, therefore the enum concept was new to me. If you are right, could we mention in the tutorial that it is actually not possible to write an equivalent solution with if/else? Certainly, I can't be the only one who was a bit puzzled. Cheers.

@gamazeps
Copy link
Contributor

gamazeps commented Nov 5, 2014

@lgrahl Thanks a lot for the remark, it's exactly the kind of comment needed, if you see others don't hesitate to open new issues :)

I'll try to add this tonight or tomorrow.

lnicola pushed a commit to lnicola/rust that referenced this issue Sep 25, 2024
internal: Disable GitHub releases for now

These are currently throwing `Error: HttpError: Resource not accessible by integration` because of the organization change, let's disable them for today's release.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants