Skip to content

Instantly share code, notes, and snippets.

@AaronM04
Created February 24, 2019 00:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AaronM04/2171dcf25f009710a72dc730d79a30c3 to your computer and use it in GitHub Desktop.
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
/// 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