Skip to content

[08_enums/enum2.rs] Messages array vs individual Message variables #2307

@mihemihe

Description

@mihemihe

08_enums/enum2.rs code defines an array of messages:

    let messages = [
        Message::Resize {
            width: 10,
            height: 30,
        },
        Message::Move(Point { x: 10, y: 15 }),
        Message::Echo(String::from("hello world")),
        Message::ChangeColor(200, 255, 255),
        Message::Quit,
    ];

For newcomers, this syntax can be confusing,especially without type inlay hints.

It looks very similar to a single struct initialization rather than an array of multiple enum variants. The messages variable can easily be mistaken for one instance of a Message instead of a list of different ones.

If someone is learning Rust enum syntax for the first time, the messages array initialization resembles a struct initialization (which users have it fresh in memory because they are the preceding exercises).

I think the exercise will benefit from having some individual definitions first, and then the messages array.

Something like:

   let move_point = Message::Move(Point { x: 100, y: 50 });
   let echo = Message::Echo(String::from("Comms up"));

   let messages = [
        Message::Resize {
            width: 10,
            height: 30,
        },
        Message::Move(Point { x: 10, y: 15 }),
        Message::Echo(String::from("hello world")),
        Message::ChangeColor(200, 255, 255),
        Message::Quit,
    ];

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions