4 ms·
By co-incidence, I started reading the updated version of this book today! Fantastic resource. Question to experienced Rust people: What are some cool use-case
by manish_gill 5y ago
By co-incidence, I started reading the updated version of this book today! Fantastic resource.
Question to experienced Rust people: What are some cool use-cases that you've seen for Macros?
- School-Cotton 5y agoSerde is really the killer app for rust macros IMO
- dagmx 5y agoSerde is honestly just so amazing. It amazes a lot of people when I demo rust and show how trivial it is to read and write our existing configuration and data files
- skohan 5y agoI'm using proc macros to generate a parser implementation from a context-free grammar, I find it pretty neat.
- loeg 5y agoThis is probably a relatively boring use case -- the C preprocessor could do basically the same thing -- but I've used macros to obviate some repetitive code in API bindings and FFIs.
- bobbylarrybobby 5y agoI assume you're asking about authoring one's own `macro_rules!`? I find them handy when writing tests (especially integration tests) to create sub-modules that all function the same way, just with different input-output pairs. This is better than using functions because each module can contain multiple functions to test. So instead of `test1a(input1, output1); test1b(input1, output1)`, I have a macro `test_input_output!(test1, input1, output1)` which generates `mod test1` containing the individual tests.
- zxv 5y agoTo me, the coolest is sqlx. It uses macros to do static type checking on plain SQL statements. It verifies types of bound parameters as well as results. It's async, which plays nicely with the leading web frameworks, etc. [0] https://docs.rs/sqlx/0.5.9/sqlx/macro.query_as.html https://docs.rs/sqlx/0.5.9/sqlx/macro.query_as.html
- davidatbu 5y agoDiesel is the leading ORM for Rust and they allow you to build SQL queries in a type safe way, as well as, making sure the struct you deserialize to matches the output of the query (ie, type safe deserialization). I've used ActiveRecord and Django's ORM, and neither provide anything like this. The closest I've seen is SQLModel, a library on top of SQLAlchemy built by the author of FastAPI. However, last I checked, the kinds of queries that Diesel allows are more restricted than that of Django or ActiveRecord. Of course , you can always write custom SQL for things like that when using Diesel. I believe also that Diesel is either maintained by or was created by the same peeps behind ActiveRecord. I also read somewhere that a running joke among them is that they learnt what not to do in writing an ORM through ActiveRecord. A question I have for y'all is: can anyone make a comparison between Diesel and TypeORM?