Skip to content

Implement Index for &[T] #16529

Closed
Closed
@kennytm

Description

@kennytm

As titled. Although slices support the a[b] operator, it does not implement Index<uint, T>, so one cannot pass in a slice in the following example:

fn do_something<V: Clone, T: Collection + Index<uint, V>>(t: &T) -> Vec<V> {
    let mut v = Vec::with_capacity(t.len());
    for i in range(0, t.len()) {
        v.push((*t)[i].clone());
    }
    v
}

fn main() {
    println!("{}", do_something(&vec![1u,2,3]));  // works
    println!("{}", do_something(&&[1u,2,3])); // does not work
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions