nlist_pat

Macro nlist_pat 

Source
macro_rules! nlist_pat {
    ($($patterns:tt)*) => { ... };
}
Expand description

Macro for using NList in patterns.

This macro uses the same syntax as array patterns, with the limitation that it only supports .. patterns at the end.

§Alternatives

The unlist macro allows destructuring NList by value in some contexts where this macro can’t be used, refer to its docs for more details.

§Example

§Destructuring

use nlist::{nlist, nlist_pat};
 
// destructuring by value
{
    let nlist_pat![a, b, c @ ..] = nlist![3, 5, 8, 13, 21];
     
    assert_eq!(a, 3);
    assert_eq!(b, 5);
    assert_eq!(c, nlist![8, 13, 21]);
}
 
// destructuring by reference
{
    let nlist_pat![a, b, c @ ..] = &nlist![3, 5, 8, 13, 21];
     
    assert_eq!(a, &3);
    assert_eq!(b, &5);
    assert_eq!(c, &nlist![8, 13, 21]);
}
 
// destructuring by mutable reference
{
    let nlist_pat![a, b, c @ ..] = &mut nlist![3, 5, 8, 13, 21];
     
    assert_eq!(a, &mut 3);
    assert_eq!(b, &mut 5);
    assert_eq!(c, &mut nlist![8, 13, 21]);
}

§Pattern matching

use nlist::{NList, PeanoInt, PlusOne, nlist, nlist_pat};
 
assert!(starts_with_zero(&nlist![0, 1, 2]));
assert!(!starts_with_zero(&nlist![1]));
assert!(!starts_with_zero(&nlist![1, 2, 3]));
 
const fn starts_with_zero<L: PeanoInt>(nlist: &NList<u32, PlusOne<L>>) -> bool {
    matches!(nlist, nlist_pat![0, ..])
}