Re:Rust 1.43
by majewsky on Thursday April 23, 2020 @04:05PM
(#22960564)
Here's a real-world example from the code I was working on yesterday: I have several database tables where the records have numeric IDs, and instead of having the ID type be just an int everywhere, I want them to be proper types to avoid mixups. Instead of
where I could easily provide the arguments in the wrong order, I want
where this cannot happen. Each ID type is just a one-element tuple containing an i64, which would just be
and so on. But in order for them to behave as expected (e.g. with the == operator or when reading/writing IDs in the database), I need to write a bunch of boilerplate. This is the macro that I have now:
This is pretty much the most basic usecase for macros. You can do some more interesting stuff than just take a name and insert it into some template, because macros can actually pattern-match the phrases given to them in the macro invocation. A good example, also from the RDBMS domain, would be https://docs.diesel.rs/diesel/macro.table.html (link goes to the documentation of the macro).
[ Parent
| on HN ]
There are no comments on this comment yet.