Skip to content

ENH: Unit-like Struct declaration/usage example #2442

Closed
rust-lang/rust
#87925
@ChrisKeefe

Description

@ChrisKeefe

Hi folks! I've been having a great time self-teaching with The Book and Rustlings, but ran into a little speed bump in chapter 5-1.

The Issue

There is a section describing unit-like structs in the book, but no example of how to declare or instantiate them.

The worked example from the first edition appears to have been removed. Declaring a unit-like struct is visible in the structs1 rustling, but it's not completely clear from that example what the syntax is doing - for example, is UnitStruct is a user-defined name, or something defined in the prelude? Instantiating a unit-like struct does not seem to be shown in the Book or in Rustlings. Appendix B is amazing if you know what you're looking for, but not super helpful here.

Proposed solution

Re-introduce the worked example from edition 1, or re-introduce a simplified example with the preferred syntax:

// Instantiate a unit-like struct with a name and a semi-colon.
struct Proton;

// Use the same notation when creating an instance.
let y = Proton;

NOTE: I have no idea which syntax is more idiomatic! 😂

If this is something the maintainers/community are interested in, I'd be happy to knock together a PR. Thanks again for making Rust so accessible!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions