macro_rules! tuple_pat {
($($t:tt)*) => { ... };
}Expand description
Generate patterns for tuples.
§Syntax
tuple_pat!([#] [Pat0 [; Count], Pat1 [; Count], ... ])
[ and ] only indicate the optional content but not that they need to be input.
Similarly, ... indicates several repeated segments, rather than inputting ....
§Examples
When the number of patterns you provide is less than the number of elements of the tuple, the following elements will not be matched. For example:
use tuplez::{tuple, tuple_pat};
let tup = tuple!(3.14, "hello", 1, [9.8]);
let tuple_pat!(a, b, c) = tup;
assert_eq!(a, 3.14);
assert_eq!(b, "hello");
assert_eq!(c, 1);If you want the last pattern to match all remaining elements, you can add a # mark:
use tuplez::{tuple, tuple_pat};
let tup = tuple!(3.14, "hello", 1, [9.8]);
let tuple_pat!(# a, b, c) = tup;
assert_eq!(a, 3.14);
assert_eq!(b, "hello");
assert_eq!(c, tuple!(1, [9.8]));But this has a bad side effect, even though the number of patterns is equal to the number of elements of the tuple, the last pattern still matches a tuple containing only one element:
use tuplez::{tuple, tuple_pat};
let tup = tuple!(3.14, "hello", 1, [9.8]);
let tuple_pat!(# a, b, c, d) = tup;
assert_eq!(a, 3.14);
assert_eq!(b, "hello");
assert_eq!(c, 1);
assert_eq!(d, tuple!([9.8])); // Not `[9.8]`In this case, just remove the # mark. Or, you can also add an extra _ pattern to unpack the last tuple:
use tuplez::{tuple, tuple_pat};
let tup = tuple!(3.14, "hello", 1, [9.8]);
let tuple_pat!(# a, b, c, d, _) = tup;
assert_eq!(a, 3.14);
assert_eq!(b, "hello");
assert_eq!(c, 1);
assert_eq!(d, [9.8]);