Skip to content
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

Closed
ghost opened this issue Jul 8, 2017 · 12 comments · Fixed by rust-lang/rust#94490
Closed

Explain how enum variants are also constructors for the enum type #800

ghost opened this issue Jul 8, 2017 · 12 comments · Fixed by rust-lang/rust#94490
Milestone

Comments

@ghost
Copy link

ghost commented Jul 8, 2017

given the following enum

enum Number {
  Int(i32),
  Float(f32)
}

The following expression Number::Int fulfills the FnOnce(i32) -> Number trait.
(same for the Float variant)

Maybe there is a good spot to mention this in the new book?

@cssivision
Copy link

cssivision commented Jul 8, 2017

I also was confused about this in the book.

@carols10cents
Copy link
Member

Is this section in the enum chapter not sufficient?

We can create instances of each of the two variants of IpAddrKind like this:

let four = IpAddrKind::V4;
let six = IpAddrKind::V6;

@ghost
Copy link
Author

ghost commented Jul 8, 2017

What I mean is, specifically pointing out, that when one writes only the identifier without the arguments, it is a function.
‎I think that's something worth knowing, for when strange errors come up, mentioning

expected type `FooEnum`, found type `Fn(Baz) -> FooEnum`

Example:

enum Number {
    Integer(i32),
    Real(f32)
}

fn main() {
    let n: Number = Number::Integer; // should be Integer(3)
}

error:

|     let n: Number = Integer; // should be Integer(3)
|                     ^^^^^^^ expected enum `Number`, found fn item
|
= note: expected type `Number`
           found type `fn(i32) -> Number {Number::Integer}`

@steveklabnik
Copy link
Member

steveklabnik commented Jul 9, 2017 via email

@ghost
Copy link
Author

ghost commented Jul 9, 2017

Oh, right. I completely forgot where this originated.

@carols10cents
Copy link
Member

Yeah, map is the situation in which this is most useful; I do want to point out this idiom in the closures/iterators chapter since it's nice.

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 IpAddrKind where we say let four = IpAddrKind::V4;, since these variants don't hold any data, we don't put parens after. But even when functions don't have arguments, you still have to have the () when you call the function, and we don't have to say IpAddrKind::V4() to construct a value, so they're not functions exactly.

idk, @steveklabnik, wdyt?

@carols10cents carols10cents added this to the ch6 milestone Jul 9, 2017
@steveklabnik
Copy link
Member

steveklabnik commented Jul 10, 2017

Agree wholeheartedly, basically. I'm conflicted too.

I think it might be okay in the enums chapter...............

@carols10cents
Copy link
Member

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.

@tmandry
Copy link
Member

tmandry commented Dec 11, 2019

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?

@carols10cents
Copy link
Member

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.

@carols10cents carols10cents reopened this Dec 12, 2019
@AndreKR
Copy link

AndreKR commented Mar 23, 2020

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. :)

@carols10cents
Copy link
Member

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.

@carols10cents carols10cents modified the milestones: ch6, ch19 Aug 4, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 1, 2022
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)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 2, 2022
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants