Skip to content

Multiple Implementations of a Trait #7590

Closed
@jmgrosen

Description

@jmgrosen

According to @cmr, this should be possible:

use std::vec;

struct IndexableThing;

impl Index<uint, ~str> for IndexableThing {
    fn index(&self, rhs: &uint) -> ~str {
        (*rhs).to_str()
    }
}

impl Index<uint, ~[uint]> for IndexableThing {
    fn index(&self, rhs: &uint) -> ~[uint] {
        vec::from_fn(*rhs, |i| i)
    }
}

fn main() {
    let thing = IndexableThing;
    let thing_str: ~str = thing[5];
    println(thing_str);
    let thing_vec: ~[uint] = thing[5];
    println(fmt!("%?", thing_vec));
}

...but it isn't. I get a error: conflicting implementations for a trait, among other errors.

I'm not sure if this is supposed to be possible, but it would be a very nice feature to have.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions