Crust of Rust: Declarative Macros

preview_player
Показать описание
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.

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

Рекомендации по теме
Комментарии
Автор

For anyone watching this more recently: there's a nightly feature called macro_metavar_expr that gives you special expressions to count the number of repetitions of a metavariable, among other things. So you won't have to write that @COUNT helper macro thing.

rurunosep
Автор

when you show cargo expand, it's such an "Ooooh" moment inducing thing. Really useful! Would love to see the series continue

AlexAegisOfficial
Автор

Can you do an episode of Crust of Rust with the topic of error handling. You could explain how to manage different error/result types from multiple crates and how to bundle them up. If I'm not mistaken there are some crates like Failure or Fehler which help with error handling.

luiswirth
Автор

1:10:17 - it has to be in that order because it needs to hit the non-recursive case first. Technically [ ] satisfies both conditions so you want it to satisfy the the one that does work rather than the one that recurses back to itself.

dantenotavailable
Автор

Thank you, Jon, for a very useful stream.
Suggestion for another Crust of Rust: how to use all these Rc, Cell, RefCell, Weak, etc. After reading all the documentation I've managed to compile my code, but it ended up panicking all the time. So a good explanation about shared ownership could be useful for many, I think.

MaximGritsenko-xj
Автор

Awesome video. And can I just say that responding to comments on chat is very helpful. I did not watch the live stream but your viewers asked all of the questions I thought of during the session.

pixelx
Автор

I can't believe how good these videos are

sebastianfischer
Автор

I just love how your non-Windows machine is called "defenestration". This is so deep

leonie
Автор

These videos are so freaking helpful, thank you Jon.

bcpeinhardt
Автор

What I learned from this video is that rust really needs to provide a way to get the length of the macro arguments

squelchedotter
Автор

And the "awesomeness" continues !! Thank you so much for making these videos ! In addition to the super-human intelligence (MIT and that too PDOS !), you have an exceptional ability to demonstrate things by building upon concepts in a very approachable way. Thank you!

shashanksharma
Автор

That's awsome and helpful! Just hope Rust will later provide handy way to count

SongRocky
Автор

The Little Book of Macros is a gem. I've been trying to do this `bitflags! {` for a while now!

Dygear
Автор

I never liked vim till watching your video series. Awesome and well done. :)

masteringdigitalworld
Автор

I missed the stream but thanks for this! Please keep doing the series as long as you feel up to it; it's been helpful so far.

tsalVlog
Автор

Thanks for the awesome content. Such a pleasure to follow along.

jonas-mmem
Автор

Very good video. Thank you. If you have the time, please do more.

uncoherentramblings
Автор

Amazing content man, I always learn so much from it. Thank you

ledues
Автор

Macros in lisp are way easier to implement, because the language is represented as a recursive list.

However the parentheses get old really fast, and I don't usually write enough macros to make up for it. I prefer the rust way of creating macros, because the language can be way more expressive than lisp in general.

fabiodan
Автор

Relatively new Rustacean here (only a few crates to my name). Love this channel.

guillaumebalaine