[][src]Macro improved_slice_patterns::destructure_iter

macro_rules! destructure_iter {
    (@match_forwards, $iter:expr, ($body:expr),
            $x:ident @ .., $($rest:tt)*) => { ... };
    (@match_forwards, $iter:expr, ($body:expr),
            $variant:ident ($x:ident).., $($rest:tt)*) => { ... };
    (@match_forwards, $iter:expr, ($body:expr), .., $($rest:tt)*) => { ... };
    (@match_forwards, $iter:expr, ($body:expr), $x:pat, $($rest:tt)*) => { ... };
    (@match_backwards, $iter:expr, ($body:expr), $x:pat, $($rest:tt)*) => { ... };
    (@match_forwards, $iter:expr, ($body:expr) $(,)*) => { ... };
    (@match_backwards, $iter:expr, ($body:expr) $(,)*) => { ... };
    ($iter:expr; [$($args:tt)*] => $body:expr) => { ... };
}

Destructure an iterator using the syntax of slice_patterns.

Wraps the match body in Some if there was a match; returns None otherwise.

Contrary to slice_patterns, this allows moving out of the iterator.

A variable length pattern (x @ ..) is only allowed as the last pattern, unless the iterator is double-ended.

Example:

This code runs with edition 2018
use improved_slice_patterns::destructure_iter;

let vec = vec![Some(1), Some(2), Some(3), None];

let res = destructure_iter!(vec.into_iter();
    [Some(x), y @ .., z] => {
        // x: usize
        // y: impl Iterator<Option<usize>>
        // z: Option<usize>
        (x, y.collect::<Vec<_>>(), z)
    }
);

assert_eq!(res, Some((1, vec![Some(2), Some(3)], None)));