let_unpack

Macro let_unpack 

Source
let_unpack!() { /* proc-macro */ }
Expand description

A procedural macro to unpack like in Python programming language, where you expect array of known size, thus making it easy to unpack.

This macro consist of specifying initial variables that your given array will be unpacked to. You can also make your variable mutable, by simply adding mut before the variable name. The astrix * is used to unpack the rest of the array, resulting new array. Note, that you can use * only once in any position. The macro will expand to the let statements and named to variables you have specified. This macro can result to unexpected behavior and panicks if you don’t know the Python unpacking Use it with caution. And remember, with great power comes great responsibility.

§Examples

Use the macro to split the string:

use easify_macros::let_unpack;
let unpacking = vec![1, 2, 3];
let result = let_unpack!(*mut a, b, c = unpacking);
a.push(10);
assert_eq!(a, vec![1, 10]);
assert_eq!(b, 2);
assert_eq!(c, 3);
use easify_macros::let_unpack;
let unpacking = vec![2, 3]
let result = let_unpack!(a, *b, c = unpacking);
a.push(10);
assert_eq!(a, 2);
assert_eq!(b, vec![]);
assert_eq!(c, 3);

§Panics

The macro will cause arguments_number compile-time error if not exactly two arguments are provided.

// This will fail to compile
// let result = let_unpack!("hello,world");