reference icon indicating copy to clipboard operation
reference copied to clipboard

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

Open jplatte opened this issue 4 years ago • 0 comments

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.

jplatte avatar Mar 02 '21 16:03 jplatte