Open
Description
This is arguably quite confusing:
trait Trait {
fn abs(self) -> Self;
}
impl Trait for i64 {
fn abs(self) -> Self {
2 * self
}
}
fn main() {
let x = 42;
println!("{}", x.abs());
println!("{}", x.abs());
}
Ouput:
84
42
So the first x.abs()
call resolves to the trait method, which is then used to influence type inference to make x
an i64
. With x
being i64
, the second x.abs()
call calls the inherent method.
@rustbot label T-compiler, T-lang, A-traits, A-typesystem
Metadata
Metadata
Assignees
Labels
Area: Type inferenceArea: Trait systemArea: Type systemCategory: Discussion or questions that doesn't represent real issues.Relevant to the compiler team, which will review and decide on the PR/issue.Relevant to the language teamRelevant to the types team, which will review and decide on the PR/issue.