-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Explain how enum variants are also constructors for the enum type #800
Comments
I also was confused about this in |
Is this section in the enum chapter not sufficient?
|
What I mean is, specifically pointing out, that when one writes only the identifier without the arguments, it is a function.
Example: enum Number {
Integer(i32),
Real(f32)
}
fn main() {
let n: Number = Number::Integer; // should be Integer(3)
} error:
|
you can also use it in things like map:
```rust
let v = vec![1, 2, 3];
let v = v.into_iter().map(Number::Integer).collect();
```
|
Oh, right. I completely forgot where this originated. |
Yeah, It feels weird explaining that enum variants are functions in the closures/iterators chapter, but I also feel like it could be confusing in the enums chapter. In the example of creating the enum instances of idk, @steveklabnik, wdyt? |
Agree wholeheartedly, basically. I'm conflicted too. I think it might be okay in the enums chapter............... |
Unfortunately, the enums chapter is too far along in the print process to make any significant additions. I've tagged this as something to take a look at when we do a third edition, but I'm going to close this for now. |
This question came up today with some people I work with. Since the online book can updated anytime, can/should this be reopened as a possible addition? |
We actually ended up working an explanation on this into the Function Pointers section of chapter 19. Reopening this issue to discuss whether this section should be moved to the Enums chapter, referenced from the enums chapter, or something else to make this content easier to find and more useful, but I did want to note that we do cover it in the book now. |
Just to let you know, I just got confused by https://doc.rust-lang.org/stable/rust-by-example/error/multiple_error_types/option_result.html where they do this: |r| r.map(Some) So I googled "rust enum variant FnOnce", and this issue is the first result. :) |
Ok, I've added a bit of explanation to chapter 6, and I'm changing this issue's milestone to chapter 19 to add a backreference in chapter 19 to this new part of chapter 6. |
Update books ## nomicon 1 commits in 90993eeac93dbf9388992de92965f99cf6f29a03..f6d6126fc96ecf4a7f7d22da330df9506293b0d0 2022-02-13 12:44:12 +0900 to 2022-02-26 02:21:21 +0900 - ffi: explicitly declare hello_from_rust for C99 (rust-lang/nomicon#343) ## reference 20 commits in 70fc73a6b908e08e66aa0306856c5211312f6c05..9d289c05fce7254b99c6a0d354d84abb7fd7a032 2022-02-14 19:33:01 -0800 to 2022-02-23 08:58:20 -0800 - Fix typo in `functions.md` (rust-lang/reference#1173) - Fix CI - Unify global_asm/asm directive list - Update src/inline-assembly.md - Update src/inline-assembly.md - Fix changes unintentional reverted in d5d3d80 - Add note about operand interpolations - Sort lists, add syntax control directives - Add another missed batch suggestion - Add missed batch suggestion - Apply suggestions from code review - Add .type, .size, and .p2align - Reformat directive lists - Add `.inst` directive - Add missing directives - Add additional directives in use - Add `.fill` directive - Style fixes - Fix code block - Add supported Directives list ## book 13 commits in 67b768c0b660a069a45f0e5d8ae2f679df1022ab..3f255ed40b8c82a0434088568fbed270dc31bf00 2022-02-09 21:52:41 -0500 to 2022-02-27 21:26:12 -0500 - Add a back reference about enum variant initializer fns. Fixes rust-lang/book#800. - Update ch01-03-hello-cargo.md - ch03-05: Add definite article for the block of code - Change variable names from "slice" to "values" - Remove reference to advanced lifetime section that no longer exists - Fix link to go to the right newtype section - Remove confusing and redundant part of a sentence about newtypes - Make transition less repetitive - Correct wording about associated functions. - Remove unnecessary extern crate proc_macro - Clarify that this code is defining, not using a procedural macro - Add manual regeneration steps for cargo new test - Update Listing 11-1 to reflect current contents ## rust-by-example 11 commits in 18c0055b8aea49391e8f758a4400097999c9cf1e..2a928483a20bb306a7399c0468234db90d89afb5 2022-01-19 08:51:55 -0300 to 2022-02-28 11:36:59 -0300 - Update destructure_slice.md (rust-lang/rust-by-example#1513) - Update iter_find.md (rust-lang/rust-by-example#1512) - Add an example of collecting errors while iterating successes (rust-lang/rust-by-example#1509) - Fix broken link on asm (rust-lang/rust-by-example#1508) - Update abort_unwind.md (rust-lang/rust-by-example#1505) - Remove duplicate text in asm.md (rust-lang/rust-by-example#1506) - Improve asm clobber example (rust-lang/rust-by-example#1504) - Add +1 to next_age (rust-lang/rust-by-example#1503) - fix comment on into_iter() for arrays (rust-lang/rust-by-example#1502) - Added new Rust 1.58 direct format args (rust-lang/rust-by-example#1501) - documentation for cfg_panic (rust-lang/rust-by-example#1500) ## rustc-dev-guide 13 commits in 62f58394ba7b203f55ac35ddcc4c0b79578f5706..32f2a5b4e7545318846185198542230170dd8a42 2022-02-11 08:42:50 -0500 to 2022-03-01 10:45:24 -0600 - Add architecture suggestion for Apple silicon (rust-lang/rustc-dev-guide#1320) - cargo timings has been stabilized (rust-lang/rustc-dev-guide#1319) - Add known-bug header. (rust-lang/rustc-dev-guide#1311) - Fix typo (rust-lang/rustc-dev-guide#1315) - Typo (rust-lang/rustc-dev-guide#1313) - instrument-coverage has been stabilized. - symbol-mangling-version has been stabilized - Fix `Ty` link (rust-lang/rustc-dev-guide#1308) - Edit glossary (rust-lang/rustc-dev-guide#1302) - Fix heading levels in the query chapter (rust-lang/rustc-dev-guide#1305) - Fix link - Edit "Queries" chapter (rust-lang/rustc-dev-guide#1301) - Link to The Rust Performance Book (rust-lang/rustc-dev-guide#1300) ## edition-guide 1 commits in beea0a3cdc3885375342fd010f9ad658e6a5e09a..c55611dd6c58bdeb52423b5c52fd0f3c93615ba8 2021-12-05 07:06:45 -0800 to 2022-02-21 14:21:39 +0100 - Remove `+nightly` for `cargo new` (rust-lang/edition-guide#276)
Update books ## nomicon 1 commits in 90993eeac93dbf9388992de92965f99cf6f29a03..f6d6126fc96ecf4a7f7d22da330df9506293b0d0 2022-02-13 12:44:12 +0900 to 2022-02-26 02:21:21 +0900 - ffi: explicitly declare hello_from_rust for C99 (rust-lang/nomicon#343) ## reference 20 commits in 70fc73a6b908e08e66aa0306856c5211312f6c05..9d289c05fce7254b99c6a0d354d84abb7fd7a032 2022-02-14 19:33:01 -0800 to 2022-02-23 08:58:20 -0800 - Fix typo in `functions.md` (rust-lang/reference#1173) - Fix CI - Unify global_asm/asm directive list - Update src/inline-assembly.md - Update src/inline-assembly.md - Fix changes unintentional reverted in d5d3d80 - Add note about operand interpolations - Sort lists, add syntax control directives - Add another missed batch suggestion - Add missed batch suggestion - Apply suggestions from code review - Add .type, .size, and .p2align - Reformat directive lists - Add `.inst` directive - Add missing directives - Add additional directives in use - Add `.fill` directive - Style fixes - Fix code block - Add supported Directives list ## book 13 commits in 67b768c0b660a069a45f0e5d8ae2f679df1022ab..3f255ed40b8c82a0434088568fbed270dc31bf00 2022-02-09 21:52:41 -0500 to 2022-02-27 21:26:12 -0500 - Add a back reference about enum variant initializer fns. Fixes rust-lang/book#800. - Update ch01-03-hello-cargo.md - ch03-05: Add definite article for the block of code - Change variable names from "slice" to "values" - Remove reference to advanced lifetime section that no longer exists - Fix link to go to the right newtype section - Remove confusing and redundant part of a sentence about newtypes - Make transition less repetitive - Correct wording about associated functions. - Remove unnecessary extern crate proc_macro - Clarify that this code is defining, not using a procedural macro - Add manual regeneration steps for cargo new test - Update Listing 11-1 to reflect current contents ## rust-by-example 11 commits in 18c0055b8aea49391e8f758a4400097999c9cf1e..2a928483a20bb306a7399c0468234db90d89afb5 2022-01-19 08:51:55 -0300 to 2022-02-28 11:36:59 -0300 - Update destructure_slice.md (rust-lang/rust-by-example#1513) - Update iter_find.md (rust-lang/rust-by-example#1512) - Add an example of collecting errors while iterating successes (rust-lang/rust-by-example#1509) - Fix broken link on asm (rust-lang/rust-by-example#1508) - Update abort_unwind.md (rust-lang/rust-by-example#1505) - Remove duplicate text in asm.md (rust-lang/rust-by-example#1506) - Improve asm clobber example (rust-lang/rust-by-example#1504) - Add +1 to next_age (rust-lang/rust-by-example#1503) - fix comment on into_iter() for arrays (rust-lang/rust-by-example#1502) - Added new Rust 1.58 direct format args (rust-lang/rust-by-example#1501) - documentation for cfg_panic (rust-lang/rust-by-example#1500) ## rustc-dev-guide 13 commits in 62f58394ba7b203f55ac35ddcc4c0b79578f5706..32f2a5b4e7545318846185198542230170dd8a42 2022-02-11 08:42:50 -0500 to 2022-03-01 10:45:24 -0600 - Add architecture suggestion for Apple silicon (rust-lang/rustc-dev-guide#1320) - cargo timings has been stabilized (rust-lang/rustc-dev-guide#1319) - Add known-bug header. (rust-lang/rustc-dev-guide#1311) - Fix typo (rust-lang/rustc-dev-guide#1315) - Typo (rust-lang/rustc-dev-guide#1313) - instrument-coverage has been stabilized. - symbol-mangling-version has been stabilized - Fix `Ty` link (rust-lang/rustc-dev-guide#1308) - Edit glossary (rust-lang/rustc-dev-guide#1302) - Fix heading levels in the query chapter (rust-lang/rustc-dev-guide#1305) - Fix link - Edit "Queries" chapter (rust-lang/rustc-dev-guide#1301) - Link to The Rust Performance Book (rust-lang/rustc-dev-guide#1300) ## edition-guide 1 commits in beea0a3cdc3885375342fd010f9ad658e6a5e09a..c55611dd6c58bdeb52423b5c52fd0f3c93615ba8 2021-12-05 07:06:45 -0800 to 2022-02-21 14:21:39 +0100 - Remove `+nightly` for `cargo new` (rust-lang/edition-guide#276)
given the following enum
The following expression
Number::Int
fulfills theFnOnce(i32) -> Number
trait.(same for the Float variant)
Maybe there is a good spot to mention this in the new book?
The text was updated successfully, but these errors were encountered: