-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Add enum namespacing to the Guide. #19616
Conversation
Notice that we need both the enum name and the variant name: `StringResult::StringOK`, but | ||
we didn't need to with `Ordering`, we just said `Greater` rather than `Ordering::Greater`. | ||
There's a reason: the Rust prelude imports the variants of `Ordering` as well as the enum | ||
itself. We can use the `use` keyword to do something similar with `StringResult`: |
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.
The current use of Ordering
the prelude is currently a subject of a bit of debate (rust-lang/rfcs#497, rust-lang/rfcs#504), would it be possible to write this without referencing the prelude or Ordering
at all?
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 would love to have it removed, but until then, the guide is still deficient. What's the timeline on the RFC getting accepted? I'd rather not have the guide be wrong for weeks.
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.
Oh I was thinking that we could accept this now if an example other than Ordering
was used. Perhaps Option
which will likely always be in the prelude?
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 chose Ordering for a few reasons. One is that it's a classic pattern in C which is different than in Rust. Secondly because it fits nicely into the classic guessing game example. Removing the Ordering example invovles changing almost the first half of the guide.
We do talk about Option in the section with variants, which we haven't gotten to yet at this point, IIRC. Or at least, that's why we do Ordering first: enum without variants, then one with.
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.
Ok! We can revise slightly as necessary as the prelude changes later on in that case.
@@ -1160,6 +1160,55 @@ Where a `StringResult` is either an `StringOK`, with the result of a computation | |||
`ErrorReason` with a `String` explaining what caused the computation to fail. These kinds of | |||
`enum`s are actually very useful and are even part of the standard library. | |||
|
|||
Enum variants are namespaced under the enum names. For example, here is an example of using |
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.
Just a wee tidbit - I think it might be unnecessary to use "example" twice in
For example, here is an example...
Perhaps this sentence could just be "Here is an example using our StringResult
:" ?
Closes #19556.