← All Exercises

Rust Declarative Macros

advancedrust~35min

Background

Rust macros enable compile-time code generation, used for DSLs, derive macros, and reducing boilerplate.

Problem

Create a macro that generates a hashmap from key-value pairs and a macro for conditional logging.

Input

Key-value pairs to insert into a hashmap.

Output

A HashMap populated with the provided pairs.

Sample tests

Sample 1

Input: hashmap!("a" => 1, "b" => 2)
Expected: {"a":1, "b":2}

Your solution

Learning objective

Learn declarative macros, pattern matching syntax, repetition with $(), and macro hygiene.