[rust-dev] conflicting implementations for trait

Erick Tryzelaar erick.tryzelaar at gmail.com
Wed Jul 2 08:50:27 PDT 2014


If you do want to do this, you can wrap the type in a struct, as in:

```
use foo_mod::MyTrait;

mod foo_mod {
pub trait MyTrait {
fn foo (&self) -> int;
}

impl MyTrait for int {
fn foo(&self) -> int {
return self + 2;
 }
}

impl MyTrait for ~str {
fn foo(&self) -> int {
 return 42;
}
}
}

struct MyInt(int)

impl MyTrait for MyInt {
fn foo(&self) -> int {
                let MyInt(i) = *self;
return i - 2;
 }
}
```

Rust will optimize away the wrapper struct, so there's no overhead in doing
this.



On Tue, Jul 1, 2014 at 3:17 PM, Steve Klabnik <steve at steveklabnik.com>
wrote:

> You cannot.
> _______________________________________________
> Rust-dev mailing list
> Rust-dev at mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140702/1cac2cd0/attachment.html>


More information about the Rust-dev mailing list