January 2018
Beginner to intermediate
454 pages
10h 8m
English
In a macro pattern, it is also possible to match against an unlimited number of patterns, using the repetition operators + and *. They behave exactly like the same operators in regular expressions:
Let's write a very useful macro, a macro to provide syntactic sugar to create HashMaps:
Note: A HashMap is a data structure from Rust's standard library that maps keys to values.
macro_rules! hash { ($( $key:expr => $value:expr ),*) => {{ let mut hashmap = ::std::collections::HashMap::new(); $(hashmap.insert($key, $value);)* hashmap }}; }
As we can see, we use the * operator here. The comma before it specify the separator token: this token must be present between each occurrence ...