This is not a particularly practical question – I just stumbled across this in Rust that I was surprised compiles, and want to understand more what is going on.
It appears that you can make an enum with a tuple variant AND implement a method with the same name (and signature, although that doesn’t appear to matter).
#[derive(Debug)]
enum Test{
Foo,
Bar(u64)
}
impl Test {
/// This function won't get called
pub fn Bar(x: u64) -> Self {
println!("In function {:?}", x);
Self::Foo
}
}
fn main() {
let a = Test::Bar(10);
println!("{:?}", a)
}
Since it does compile it led me to suspect that the method must somehow live in a different “namespace” to the variant as far as the compiler is concerned, which makes me suspect there would be a way to disambiguate the call. I couldn’t find anything in the documentation or by searching though.