The following seems to work:

trait Double {
    fn double(&self) -> Self;
}

impl Double for int {
    fn double(&self) -> int {
        *self * 2
    }
}

fn main() {
    let x = 2;
    println!("{}", x.double()); // prints "4"
}



--
Ziad


On Tue, Nov 5, 2013 at 1:29 PM, Steven Blenkinsop <steven...@gmail.com>wrote:

> As long as "you" are the person who owns the type, yeah, but I suspect
> that's not what you mean. Coherence requires that you only implement traits
> for types if you own either the trait or the type (or both). You can't
> implement a 3rd party trait for a 3rd party type, since then there could be
> multiple such implementations for a given (trait, type) pair, and coherence
> would be broken.
>
>
> On Tue, Nov 5, 2013 at 2:28 PM, Ziad Hatahet <hata...@gmail.com> wrote:
>
>> On Tue, Nov 5, 2013 at 9:17 AM, Patrick Walton <pcwal...@mozilla.com>wrote:
>>
>>> On 11/5/13 2:44 AM, spir wrote:
>>>
>>>> Why not just add a declaration of the trait at the top of the struct
>>>> type def?
>>>>
>>>> struct PairList<Val> : Iterable {
>>>>
>>>
>>> You can implement traits on types that aren't structs.
>>
>>
>>
>> Isn't another effect of this is the ability to "monkey-patch" structs to
>> implement extra methods or traits? E.g. you can later in implement a
>> to_str() method for a type, or implement certain traits, like Clone or Drop.
>>
>>
>> --
>> Ziad
>>
>>
>> _______________________________________________
>> Rust-dev mailing list
>> Rust-dev@mozilla.org
>> https://mail.mozilla.org/listinfo/rust-dev
>>
>>
>
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to