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

Page on structs implies that unit struct definition is the same as struct struct definition named constant #971

Open
jplatte opened this issue Mar 2, 2021 · 0 comments

Comments

@jplatte
Copy link

jplatte commented Mar 2, 2021

From https://doc.rust-lang.org/reference/items/structs.html:

A unit-like struct is a struct without any fields, defined by leaving off the list of fields entirely. Such a struct implicitly defines a constant of its type with the same name. For example:

struct Cookie;
let c = [Cookie, Cookie {}, Cookie, Cookie {}];

is equivalent to

struct Cookie {}
const Cookie: Cookie = Cookie {};
let c = [Cookie, Cookie {}, Cookie, Cookie {}];

Which seems to imply that a curly-brace struct / struct struct definition with no fields a named constant of the same name is exactly the same thing as a unit struct definiton. However, this is not true when it comes to patterns: Cookie can be used as a pattern with the first declaration, but not with the second.

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

No branches or pull requests

1 participant