Skip to content

Matching on a struct variant enum #40666

Closed
@nicolaction

Description

@nicolaction

I have been playing with rust enums recently, but I found that I can't seem to make them work the way I expect.

I defined struct variants for an enum:

pub enum Entity {
    Character {
        money: f64,
        ....
    },
    ...
}

And I want to match on them. If I match on self, in the module, I get this:

match self {
    Entity::Character(entity) => {
        entity.money = ...

But I get the following error message:

error[E0532]: expected tuple struct/variant, found struct variant types::Entity::Character

What to do? Am I doing this wrong?

PS: I know I can destructure the content of the enum, but I want to use it as if it was defined locally with the correct variant.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions