Crust of Rust: Declarative Macros
Channel: Jon Gjengset
Date: 2020-04-29

In this second Crust of Rust video, we cover declarative macros, macro_rules!, by re-implementing the vec! macro from the standard library. As part of that, we cover not only how to write these, but some of the gotchas and tricks you'll run into, and some common use-cases.

You can find the final code at https://gist.github.com/jonhoo/ec57882a976a2d2a92b3138ea25cd45a, and the Little Book of Rust Macros at https://danielkeep.github.io/tlborm/book/index.html.

0:00:00 Introduction
0:01:35 The vec macro
0:04:08 The Little Book of Rust Macros
0:05:17 Macro syntax and hygiene
0:16:42 The empty vector
0:19:26 Non-empty vectors
0:25:50 Macros v2
0:26:34 Macro delimiters
0:27:54 Declarative vs procedural macros
0:30:15 Repeated macro arguments
0:39:49 Trailing commas
0:44:10 Why are macros useful?
0:47:29 Vector by repetition
0:51:02 Macro rules readability
0:52:00 Invalid macro inputs
0:54:52 Test that something doesn't compile
0:56:50 Tidying up the patterns
0:59:05 Reallocations for repetition constructor
1:04:08 Macro argument trait bounds
1:06:40 "use" hygiene in macros
1:08:28 The standard library pattern
1:10:20 The need for counting
1:11:38 Eager macro errors
1:13:00 Counting in macros
1:24:48 Other ways of counting
1:27:27 Ensuring count is computed at compile time
1:28:32 Hiding internal macro patterns
1:31:13 Other collection literals
1:33:00 Comparing against the std implementation

You can watch the live version with comments at https://www.youtube.com/watch?v=UIY75ohrqKQ