January 2019
Beginner to intermediate
554 pages
13h 31m
English
Apart from token tree types, we also need a way to repeatedly generate certain parts of our code. One of the practical examples from the standard library is the vec![] macro, which relies on repetition to give an illusion of variadic arguments, and allows you to create Vecs in any of the following manners:
vec![1, 2, 3];vec![9, 8, 7, 6, 5, 4];
Let's see how vec! does this. Here's vec's macro_rules! definition from the standard library:
macro_rules! vec { ($elem:expr; $n:expr) => ( $crate::vec::from_elem($elem, $n) ); ($($x:expr),*) => ( <[_]>::into_vec(box [$($x),*]) ); ($($x:expr,)*) => (vec![$($x),*])}
By ignoring the details to the right of => and focusing on the last two matching rules on the left-hand side, we can ...
Read now
Unlock full access