Rust errors / Names, paths and imports
Rust compiler error · checked with rustc 1.99.0
Rust error E0034: multiple applicable items in scope
What it meansThe compiler doesn't know what method to call because more than one method has the same prototype.
What rustc prints
error[E0034]: multiple applicable items in scope --> main.rs:15:11 | 15 | Test::foo() // error, which foo() to call? | ^^^ multiple `foo` found | note: candidate #1 is defined in an impl of the trait `Trait1` for the type `Test` --> main.rs:11:24 | 11 | impl Trait1 for Test { fn foo() {} } | ^^^^^^^^ note: candidate #2 is defined in an impl of the trait `Trait2` for the type `Test` --> main.rs:12:24 | 12 | impl Trait2 for Test { fn foo() {} } | ^^^^^^^^ help: use fully-qualified syntax to disambiguate | 15 - Test::foo() // error, which foo() to call? 15 + <Test as Trait1>::foo() // error, which foo() to call? | 15 - Test::foo() // error, which foo() to call? 15 + <Test as Trait2>::foo() // error, which foo() to call? |
Real output of rustc 1.99.0 compiling the official example below (2021 edition). Your names and line numbers will differ; the first line is the part to match.
The compiler team's explanation
The compiler doesn't know what method to call because more than one method has the same prototype.
Erroneous code example:
struct Test;
trait Trait1 {
fn foo();
}
trait Trait2 {
fn foo();
}
impl Trait1 for Test { fn foo() {} }
impl Trait2 for Test { fn foo() {} }
fn main() {
Test::foo() // error, which foo() to call?
}
To avoid this error, you have to keep only one of them and remove the others. So let's take our example and fix it:
struct Test;
trait Trait1 {
fn foo();
}
impl Trait1 for Test { fn foo() {} }
fn main() {
Test::foo() // and now that's good!
}
However, a better solution would be using fully explicit naming of type and trait:
struct Test;
trait Trait1 {
fn foo();
}
trait Trait2 {
fn foo();
}
impl Trait1 for Test { fn foo() {} }
impl Trait2 for Test { fn foo() {} }
fn main() {
<Test as Trait1>::foo()
}
One last example:
trait F {
fn m(&self);
}
trait G {
fn m(&self);
}
struct X;
impl F for X { fn m(&self) { println!("I am F"); } }
impl G for X { fn m(&self) { println!("I am G"); } }
fn main() {
let f = X;
F::m(&f); // it displays "I am F"
G::m(&f); // it displays "I am G"
}From rustc --explain E0034 (1.99.0), part of the Rust project, MIT or Apache-2.0. Official page.
Related errors
Up next
Often hit nextE0425: cannot find value in this scopeA related error from the same family.Read it next →Did this answer your question?