trait

SliceIndex

Use a generic bound to accept both element positions and ranges in a checked slice API. The associated Output differs with the index type. SliceIndex is sealed; use standard implementations and slice.get rather than calling its unstable methods directly. Expected stdout (not executed): Some(6) Some([6, 9]) Common mistake: Trying to implement the sealed trait for an arbitrary custom index type.

Formal Syntax & Structure

rust syntaxrust
I: SliceIndex<[T]>

Working Example

Example Snippetrust
fn main() {    use std::slice::SliceIndex;    fn lookup<T, I: SliceIndex<[T]>>(data: &[T], i: I) -> Option<&I::Output> { data.get(i) }    let data = [3, 6, 9];    println!("{:?} {:?}", lookup(&data, 1), lookup(&data, 1..3));}
Rust Reference