Trait core::ops::IndexExperimental [-]  [+] [src]

pub trait Index<Index: ?Sized, Result: ?Sized>: ?Sized {
    fn index<'a>(&'a self, index: &Index) -> &'a Result;
}

The Index trait is used to specify the functionality of indexing operations like arr[idx] when used in an immutable context.

Example

A trivial implementation of Index. When Foo[Foo] happens, it ends up calling index, and therefore, main prints Indexing!.

#[deriving(Copy)] struct Foo; impl Index<Foo, Foo> for Foo { fn index<'a>(&'a self, _index: &Foo) -> &'a Foo { println!("Indexing!"); self } } fn main() { Foo[Foo]; }
#[deriving(Copy)]
struct Foo;

impl Index<Foo, Foo> for Foo {
    fn index<'a>(&'a self, _index: &Foo) -> &'a Foo {
        println!("Indexing!");
        self
    }
}

fn main() {
    Foo[Foo];
}

Required Methods

fn index<'a>(&'a self, index: &Index) -> &'a Result

The method for the indexing (Foo[Bar]) operation

Implementors