Created
February 24, 2019 00:34
-
-
Save AaronM04/2171dcf25f009710a72dc730d79a30c3 to your computer and use it in GitHub Desktop.
Rust does not compare function argument types when determining which trait's implementation to use
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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