Function gen_slice

Source
pub fn gen_slice<T, F>(
    n: usize,
    f: F,
) -> impl DoubleEndedIterator<Item = T> + ExactSizeIterator + FusedIterator
where for<'a> F: FnMut(&[bool]) -> T,
Expand description

Returns an Iterator producing T for each possible combinations of n bools.

Each combination is mapped from &[bool] -> T using f, where f receives &[bool] where len() == n.

See also gen_const() for const generic variant.

See also implementation for more information about the Iterator implementation.

§Memory

The returned Iterator stores a [bool; MAX] on the stack.

Alternatively, gen_vec_slice() stores a Vec<bool>::with_capacity(n) on the stack.

See also gen_with_buffer() for using a custom buffer.

§Panics

Panics if n is larger than the MAX number of supported variables. However, you likely have other problems, if you encounter this.

See also count(n) and is_supported(n).

§Example

See crate root for more examples.

let n = 2;
let combinations = truth_values::gen_slice(n, |bools| {
    match bools {
        &[a, b] => (a, b),
        _ => unreachable!(),
    }
})
.collect::<Vec<_>>();

assert_eq!(
    combinations,
    [
        (false, false),
        (true, false),
        (false, true),
        (true, true),
    ]
);