Skip to content

Mention struct-like variants in enums2 hint instead of anonymous structs#2353

Open
orzechow wants to merge 2 commits intorust-lang:mainfrom
orzechow:improve_enums2_hint
Open

Mention struct-like variants in enums2 hint instead of anonymous structs#2353
orzechow wants to merge 2 commits intorust-lang:mainfrom
orzechow:improve_enums2_hint

Conversation

@orzechow
Copy link
Copy Markdown

The current hint of the enums2 exercise uses an outdated term for struct-like enum variants, which is confusing to Rust newbies:

You can create enumerations that have different variants with different types such as anonymous structs, structs, a single string, tuples, no data, etc.

The term anonymous struct seems outdated. It's neither mentioned in the Rust book on structs or enums, nor the references on structs or enums.
A web search rather points to a Rust Forum thread hinting

Don’t assume that there is such a thing as “the anonymous struct feature” at all

The only place I found this term, is in the Rust docs on enums, but not in the docs on structs.
It seems like Rust used this term once, but not anymore. See also #1204 and the 2018 edition Rust book on enums.

Instead the book on enums now calls it named fields, like a struct.
The reference on enums uses the term struct-like enum variant.

Therefore, I'd suggest updating this hint to struct-like enum variants.

@orzechow
Copy link
Copy Markdown
Author

@senekor
Copy link
Copy Markdown
Contributor

senekor commented Apr 18, 2026

I think the list of possible variants is still not correct. There are three possibilities, syntactically:

  • struct-like: enum E { Variant { field: i32 } }
  • tuple: enum E { Variant(i32) }
  • unit: enum E { Variant }

Not sure about the names, but these are the three syntactic possibilities that should be reflected.

@orzechow
Copy link
Copy Markdown
Author

Thanks for the feedback!

I agree, let's cleanup the list as well.
The reference uses terms like

  • struct-like [enum] variant
  • tuple-like variant
  • unit-like variant, unit-only enum

See f9f8a37

@senekor Shall I also add those code snippets, you suggested? Like this:

hint = """
You can create enumerations that have different variants with different types
such as struct-like `enum E { Variant { field: i32 } }`,
tuple-like `enum E { Variant(i32) }` and unit-only `enum E { Variant }` variants."""

@senekor
Copy link
Copy Markdown
Contributor

senekor commented Apr 30, 2026

Good idea to check the reference for what terms it uses. If the reference uses both, I would lean toward using "unit-like" instead of "unit-only", for uniformity. But both are fine.

Reading over that hint again, the expression "variants with different types" also bugs me a little. The three possibilities "struct-like", "tuple-like" and "unit-like" aren't "types" in the context of a programming language... They are rather syntactic elements used to define a type.

I took a stab at rewriting the hint. What do you think, is it too much?

Enum variants can be defined using three different forms: "struct-like",
"tuple-like" and "unit-like". Struct-like variants contain named fields, whereas
tuple-like variants have unnamed fields. Unit-like variants don't have any
fields. Here's an example enum definition, which uses all three forms:

enum EnumUsingAllVariantForms {
    StructLike { named_field: bool },
    TupleLike(bool),
    UnitLike
}

@orzechow your commits show up as unverified btw., you might want to check your signing config :)

@orzechow
Copy link
Copy Markdown
Author

Agree, let's be precise.

type could be confusing – we're talking about variants of one struct.

Also, after re-reading the reference, an enum is called unit-only if it only contains unit variants. So, using unit-like for the variant is much better, yes.

I'd keep the code example and condense the description to:

Enum variants can be defined using three different forms: struct-, tuple- and 
unit-like. Here's an example enum definition, which uses all three forms:

enum EnumUsingAllVariantForms {
    StructLike { named_field: bool },
    TupleLike(bool),
    UnitLike
}

@senekor What do you think?

PS: Thanks for the signing hint – that shouldn't have happened…

@senekor
Copy link
Copy Markdown
Contributor

senekor commented Apr 30, 2026

I like this version a lot. @mo8it what do you think?

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

Successfully merging this pull request may close these issues.

3 participants