Function konst::slice::iter

source ·
pub const fn iter<T>(slice: &[T]) -> Iter<'_, T>
Expand description

Gets a const iterator over slice, const equivalent of <[T]>::iter

§Example

§Normal

use konst::iter::for_each;
use konst::slice;

const ARR: &[usize] = &{
    let mut arr = [0usize; 3];
    // the `slice::iter` call here is unnecessary,
    // you can pass a slice reference to `for_each`
    for_each!{(i, elem) in slice::iter(&["foo", "hello", "That box"]), enumerate() =>
        arr[i] = elem.len();
    }
    arr
};

assert_eq!(ARR, [3, 5, 8]);

§Reversed

use konst::iter::for_each;
use konst::slice;

const ARR: &[usize] = &{
    let mut arr = [0usize; 3];
    for_each!{(i, elem) in slice::iter(&["foo", "hello", "That box"]).rev(),enumerate() =>
        arr[i] = elem.len();
    }
    arr
};

assert_eq!(ARR, [8, 5, 3]);