Skip to content

'Self' cannot be used in enums #49636

@anirudhb

Description

@anirudhb

I am trying to create a method on an enum, like this:

enum Foo {
    Bar(String),
    Baz
}

impl Foo {
    pub fn get_bar(self) -> Option<String> {
        match self {
//          vvvv error occurs
            Self::Bar(s) => Some(s),
            _ => None,
        }
    }
}

An error occurs: No variant named 'Bar' found for type 'foo::Foo' in the current scope

Is this intended behavior? If so, it should be made more clear. At least provide a better error message.
Honestly, if this isn't "implemented" yet, then it should be because it would make a better user experience.

edit (x2): formatting

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