Macros - The Rust Programming Language
By now you’ve learned about many of the tools Rust provides for abstracting and reusing code. These units of code reuse have a rich semantic structure. For example, functions have a type signature, type parameters have trait bounds, and overloaded functions must belong to a particular trait. This structure means that Rust’s core abstractions have powerful compile-time correctness checking. But this comes at the price of reduced flexibility. If you visually identify a pattern of repeated code, you may find it’s difficult or cumbersome to express that pattern as a generic function, a trait, or anything else within Rust’s semantics. Macros allow us to abstract at a syntactic level. A macro invocation is shorthand for an "expanded" syntactic form. This expansion happens early in compilation, before any static checking. As a result, macros can capture many patterns of code reuse that Rust’s core abstractions cannot. The drawback is that macro-based code can be harder to understand, because
Macros - The Rust Programming Language Light (default) Rust Coal Navy Ayu The Rust Programming Language Macros By now you’ve learned about many of the tools Rust provides for abstracting and reusing code. These units of code reuse have a rich semantic structure. For example, functions have a type signature, type parameters have trait bounds, and overloaded functions must belong to a particular trait. This structure means that Rust’s core abstractions have powerful compile-time correctness checking. But this comes at the price of reduced flexibility. If you visually identify a pattern of repeat
saved by
related reading
- A half-hour to learn Rustfasterthanli.me
- Methods - The Rust Programming Languagedoc.rust-lang.org
- References and Borrowing - The Rust Programming Languagedoc.rust-lang.org
- Defining Shared Behavior with Traits - The Rust Programming Languagedoc.rust-lang.org
- Closures - The Rust Programming Languagedoc.rust-lang.org
- Rust Language Cheat Sheetcheats.rs
- Generic Types, Traits, and Lifetimes - The Rust Programming Languagedoc.rust-lang.org
- The match Control Flow Construct - The Rust Programming Languagedoc.rust-lang.org
- Summary - Rust for C-Programmersrust-for-c-programmers.com
- Summary - Rust for C-Programmersrust-for-c-programmers.com
- The program structure - Rust for C-Programmersrust-for-c-programmers.com
- Variables and Mutability - The Rust Programming Languagedoc.rust-lang.org