January 2019
Beginner to intermediate
554 pages
13h 31m
English
Armed with the knowledge of repetitions and token tree types, let's build something practical using repetitions in macro_rules!. In this section, we'll build a crate that exposes a macro that allows you to create HashMaps such as the following:
let my_map = map! { 1 => 2, 2 => 3};
This is more concise and readable compared to manually calling HashMap::new(), followed by one or more insert calls. Let's create a new cargo project by running cargo new macro_map --lib with the initial block for macro_rules!:
// macro_map/lib.rs#[macro_export]macro_rules! map { // todo}
Since we want the users to use our macros, we need to add a #[macro_export] attribute on this macro definition. ...
Read now
Unlock full access