Trait std::ops::SliceExperimental [-]  [+] [src]

pub trait Slice<Idx, Result> {
    fn as_slice_(&'a self) -> &'a Result;
    fn slice_from_or_fail(&'a self, from: &Idx) -> &'a Result;
    fn slice_to_or_fail(&'a self, to: &Idx) -> &'a Result;
    fn slice_or_fail(&'a self, from: &Idx, to: &Idx) -> &'a Result;
}

The Slice trait is used to specify the functionality of slicing operations like arr[from..to] when used in an immutable context.

Example

A trivial implementation of Slice. When Foo[..Foo] happens, it ends up calling slice_to, and therefore, main prints Slicing!.

#[deriving(Copy)] struct Foo; impl Slice<Foo, Foo> for Foo { fn as_slice_<'a>(&'a self) -> &'a Foo { println!("Slicing!"); self } fn slice_from_or_fail<'a>(&'a self, _from: &Foo) -> &'a Foo { println!("Slicing!"); self } fn slice_to_or_fail<'a>(&'a self, _to: &Foo) -> &'a Foo { println!("Slicing!"); self } fn slice_or_fail<'a>(&'a self, _from: &Foo, _to: &Foo) -> &'a Foo { println!("Slicing!"); self } } fn main() { Foo[..Foo]; }
#[deriving(Copy)]
struct Foo;

impl Slice<Foo, Foo> for Foo {
    fn as_slice_<'a>(&'a self) -> &'a Foo {
        println!("Slicing!");
        self
    }
    fn slice_from_or_fail<'a>(&'a self, _from: &Foo) -> &'a Foo {
        println!("Slicing!");
        self
    }
    fn slice_to_or_fail<'a>(&'a self, _to: &Foo) -> &'a Foo {
        println!("Slicing!");
        self
    }
    fn slice_or_fail<'a>(&'a self, _from: &Foo, _to: &Foo) -> &'a Foo {
        println!("Slicing!");
        self
    }
}

fn main() {
    Foo[..Foo];
}

Required Methods

fn as_slice_(&'a self) -> &'a Result

The method for the slicing operation foo[]

fn slice_from_or_fail(&'a self, from: &Idx) -> &'a Result

The method for the slicing operation foo[from..]

fn slice_to_or_fail(&'a self, to: &Idx) -> &'a Result

The method for the slicing operation foo[..to]

fn slice_or_fail(&'a self, from: &Idx, to: &Idx) -> &'a Result

The method for the slicing operation foo[from..to]

Implementors