Enum variant and associated function with matching names compiles

Hey there! I'm posting on here because I've noticed what might be a bug of the compiler but could also just be me misunderstanding something related to how Rust works.

Lets take the example of the following enum:

enum Example {
    Example1,
    Example2,
}

If I then add the following block to the source file, the code will still compile:

impl Example {
    fn Example1() {
        println!("Hello, World!")
    }
}

I do understand that this isn't the recommended case for a function name in Rust, however I would expect that the compiler would catch the name collision regardless.

If I then try to invoke Example::Example1(), I get the following error:

`Example::Example1` is a unit enum variant, and does not take parentheses to be constructed

I can construct the enum variant without issue. So the behaviour is consistent.

However, once again, I don't really understand why the compiler doesn't flag the name collision to begin with. Is there a use case for this behaviour that I'm missing?

I've posted a Gist with two files -- one that compiles, one that doesn't -- to clarify exactly what I'm talking about.

I look forward to any and all responses!

- Noah

Seems to be tracked in this issue:

1 Like

Got it. Thanks!

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.