Skip to content

Instantly share code, notes, and snippets.

@AaronM04 AaronM04/wow.rs
Created Feb 24, 2019

Embed
What would you like to do?
Rust does not compare function argument types when determining which trait's implementation to use
/// Won't compile:
///
/// $ rustc wow.rs
/// error[E0034]: multiple applicable items in scope
/// --> wow.rs:33:9
/// |
/// 33 | foo.wow("TSecond is str");
/// | ^^^ multiple `wow` found
/// |
/// note: candidate #1 is defined in an impl of the trait `TFirst` for the type `Foo`
/// --> wow.rs:13:5
/// |
/// 13 | fn wow(&self, extra: i64) {
/// | ^^^^^^^^^^^^^^^^^^^^^^^^^
/// note: candidate #2 is defined in an impl of the trait `TSecond` for the type `Foo`
/// --> wow.rs:22:5
/// |
/// 22 | fn wow(&self, extra: &str) {
/// | ^^^^^^^^^^^^^^^^^^^^^^^^^^
///
/// error: aborting due to previous error
///
/// For more information about this error, try `rustc --explain E0034`.
#[derive(Debug)]
struct Foo{x:u64}
trait TFirst {
fn wow(&self, i64);
}
trait TSecond {
fn wow(&self, &str);
}
impl TFirst for Foo {
fn wow(&self, extra: i64) {
for i in 0..self.x {
println!("TFirst wow {} of {}", i+1, self.x);
}
println!("-- extra: {}", extra);
}
}
impl TSecond for Foo {
fn wow(&self, extra: &str) {
for i in 0..self.x {
println!("TSecond wow {} of {}", i+1, self.x);
}
println!("-- extra: {}", extra);
}
}
fn main() {
let foo = Foo{x:3};
println!("{:?}", foo);
foo.wow("TSecond is str");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.