rust-lang / rust-lang/reference

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

Open
#971 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
1.6k
Forks
607
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the unit-like structs section of the Rust Reference's structs page and compare its equivalence example with the pattern behavior described in the issue. Check the Rust language rules for unit-struct patterns and named constants; done means the page no longer implies the two declarations are interchangeable and accurately explains the distinction.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.