DEV Community

Jon Stødle
Jon Stødle

Posted on • Originally published at blog.jonstodle.com on

4 1

Things I Enjoy in Rust: Macros

Things I enjoy in Rust:

  1. Enums
  2. Error Handling
  3. Immutability
  4. Impl
  5. Macros

I had never used a programming language with macros before Rust. I’ve heard about it, and seen some rave about it, but I’ve never actually looked into what they do. Now that I know - I love them!

Macros in Rust enables what is sometimes called meta programming: code writing other code. When you use a macro in Rust, you’re actually calling a function which takes the input and generates more code based on that input. The result of this is what’s fed to the compiler.

These macros are much like keyboard macros or programmable macros in some software: they let you automate repetitive tasks.

Granted, I haven’t written any macros myself, but I love that I’m able to use convenience macros from both the standard library and from external crates.

Let’s take this example from The Book which shows how the vec! macro works:

// vec! macro defined
#[macro_export]
macro_rules! vec {
    ( $( $x:expr ),* ) => {
        {
            let mut temp_vec = Vec::new();
            $(
                temp_vec.push($x);
            )*
            temp_vec
        }
    };
} 

// vec! macro in use
let v: Vec<u32> = vec![1, 2, 3];

Enter fullscreen mode Exit fullscreen mode

The macro_rules! defines the kind of input the vec! macro accepts (a variable number of inputs), it creates a new Vec instance, and pushes the inputs into the Vec and returns it.

In addition, as of the 2018 edition of Rust, it’s been possible to create macros for use with #[derive()], which makes it possible for anyone to create macros that can act on structs. Just like how #[derive(Clone)] makes a struct implement the Clone trait, you can make a macro that automatically implements your own traits for any struct.

Hate writing tons of boiler plate code to convert your structs to JSON or convert JSON to structs? The serde crate has macros for that! With a simple `#[derive(Serialize, Deserialize)] your struct support being serialized and deserialized to JSON and back. That’s just about as simple as it gets.

With macros, there’s much to enjoy.

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more