Trait SliceFind

Source
pub trait SliceFind<T>: Slice<Item = T> {
    // Required methods
    const fn find(&self, x: &T) -> Option<usize>
       where T: PartialEq;
    const fn find_by<'a, F>(&'a self, f: F) -> Option<usize>
       where F: FnMut(&'a T) -> bool,
             T: 'a;
    const fn find_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Option<usize>
       where F: FnMut(&'a T) -> B,
             B: PartialEq,
             T: 'a;
    const fn rfind(&self, x: &T) -> Option<usize>
       where T: PartialEq;
    const fn rfind_by<'a, F>(&'a self, f: F) -> Option<usize>
       where F: FnMut(&'a T) -> bool,
             T: 'a;
    const fn rfind_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Option<usize>
       where F: FnMut(&'a T) -> B,
             B: PartialEq,
             T: 'a;
}

Required Methods§

Source

const fn find(&self, x: &T) -> Option<usize>
where T: PartialEq,

Performs a linear search for the first value that equals x.

§Example
use slice_ops::ops::*;
 
//                   v
let x = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3];
 
let i = x.find(&5).unwrap();
 
assert_eq!(i, 4);
assert_eq!(x[i], 5);
Source

const fn find_by<'a, F>(&'a self, f: F) -> Option<usize>
where F: FnMut(&'a T) -> bool, T: 'a,

Performs a linear search for the first value that satisfies the given predicate.

§Example
use slice_ops::ops::*;
 
//                      v
let x = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3];
 
let f = |&xn| xn > 5; 
 
let i = x.find_by(f).unwrap();
 
assert_eq!(i, 5);
Source

const fn find_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Option<usize>
where F: FnMut(&'a T) -> B, B: PartialEq, T: 'a,

Performs a linear search for the first value that matches the given key given a hashing function.

§Example
use slice_ops::ops::*;
 
//             v
let x = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3];
 
let f = |&xn| xn % 2;
 
let i = x.find_by_key(&0, f).unwrap();
 
assert_eq!(i, 2);
Source

const fn rfind(&self, x: &T) -> Option<usize>
where T: PartialEq,

Performs a linear search from the right for the first value that equals x.

§Example
use slice_ops::ops::*;
 
//                               v
let x = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3];
 
let i = x.rfind(&5).unwrap();
 
assert_eq!(i, 8);
assert_eq!(x[i], 5);
Source

const fn rfind_by<'a, F>(&'a self, f: F) -> Option<usize>
where F: FnMut(&'a T) -> bool, T: 'a,

Performs a linear search from the right for the first value that satisfies the given predicate.

§Example
use slice_ops::ops::*;
 
//                            v
let x = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3];
 
let f = |&xn| xn > 5;
 
let i = x.rfind_by(f).unwrap();
 
assert_eq!(i, 7);
Source

const fn rfind_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Option<usize>
where F: FnMut(&'a T) -> B, B: PartialEq, T: 'a,

Performs a linear search from the right for the first value that matches the given key given a hashing function.

§Example
use slice_ops::ops::*;
 
//                            v
let x = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3];
 
let f = |&xn| xn % 2;
 
let i = x.rfind_by_key(&0, f).unwrap();
 
assert_eq!(i, 7);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T> SliceFind<T> for [T]

Source§

fn find(&self, x: &T) -> Option<usize>
where T: PartialEq,

Source§

fn find_by<'a, F>(&'a self, f: F) -> Option<usize>
where F: FnMut(&'a T) -> bool, T: 'a,

Source§

fn find_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Option<usize>
where F: FnMut(&'a T) -> B, B: PartialEq, T: 'a,

Source§

fn rfind(&self, x: &T) -> Option<usize>
where T: PartialEq,

Source§

fn rfind_by<'a, F>(&'a self, f: F) -> Option<usize>
where F: FnMut(&'a T) -> bool, T: 'a,

Source§

fn rfind_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Option<usize>
where F: FnMut(&'a T) -> B, B: PartialEq, T: 'a,

Implementors§