[HN Gopher] The Little Book of Rust Macros ___________________________________________________________________ The Little Book of Rust Macros Author : DerekBickerton Score : 140 points Date : 2021-11-21 16:34 UTC (6 hours ago) (HTM) web link (danielkeep.github.io) (TXT) w3m dump (danielkeep.github.io) | ncmncm wrote: | Wondering when Rust macros will learn about types. | | Lisp macros, of course, know nothing about types, because, in | old-school Lisp, there are, lexically, no types. But in Rust, at | expansion time, types are usually absolutely nailed down. | (...Unless the macro is expanding to a generic definition; but | even then, there are named types, you just don't know what they | would be bound to). | | Or maybe they do understand types, now? I haven't checked lately. | alschwalm wrote: | It is definitely also worth mentioning the now-stabilized | procedural macros [1] (unless I missed where they are described). | proc macros power some of the more complex macro usage in the | rust ecosystem (like rocket and actix-webs router macros, any | sort of custom derive, etc). | | [1]: https://doc.rust-lang.org/reference/procedural-macros.html | manish_gill wrote: | 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? | umanwizard wrote: | Serde is really the killer app for rust macros IMO | dagmx wrote: | Serde 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 | loeg wrote: | This 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. | skohan wrote: | I'm using proc macros to generate a parser implementation from | a context-free grammar, I find it pretty neat. | gizmo385 wrote: | Is this open source anywhere? I'd love to take a look at the | code. A compiler is on my short-list for my next project to | use to learn rust. | jgrant27 wrote: | If you have used Rust in any real world capacity on actual | projects you know that there are no "Little Books" when its comes | to using Rust. An overly complex and unproductive language as | much as C++. | jen20 wrote: | Absolutely not my experience. My experience is there is an | initial large learning curve (2-3 weeks) and then you can be | vastly more productive than any dynamic language, with better | safety, a better library ecosystem, and better tooling. | mijamo wrote: | Both are overly broad generalization. If you are working on a | backend for a web app I would certainly not recommend Rust | and it would be a big loss of productivity compared to Python | or JavaScript, or even Go. | | For a game it could fit in some part but you would be very | limited by the ecosystem so I would also not recommend except | if you really know what you are doing and have significant | resources. | | For low level system programing it is pretty nice, and maybe | the best alternative right now. | | For a compiler I would say it depends if performance is the | main priority, in that case yes, otherwise no. | | There are plenty of other cases of course and for each the | answer would be different. | | It is not really a general purpose language that you could | use without worry for everything like Python, at least not | yet. | lokeg wrote: | Using libraries is so much easier in rust compared to many | other libraries due to rustdoc. I can trust the type | signatures to show me the actual usage, I can trust the | code snippets to not be outdated, and above all this is | consistent for all code in the entire ecosystem. | | Almost always when I use python libraries I have to get | used to a new documentation format, learn how to navigate | it and so on. And then it has to be detailed enough to make | up for the lack of type signatures. I cannot tell you how | often I have to skim a significant portion of my | dependencies' source just to use them. (Though there are | counterexamples, stdlib and numpy in particular actually | have okay docs) | the_duke wrote: | This was a great learning resource for macros in the early days | of Rust when there was almost no official documentation. | | Nowadays anything that's a bit more complex should almost | certainly use proc macros instead, which allow much saner | implementations than complex, recursive macro shenanigans. | pitaj wrote: | Absolutely disagree. If it can be done, without sacrificing | useability, without proc macros, that's the way it should be | done. Proc macros don't play well with IDEs, add compilation | time, introduce dependencies, and can be a security risk. | | There are things that can be done to improve proc macros, like | executing them in a WASM runtime, but currently they should | only be used when necessary. | tialaramex wrote: | On the other hand, procedural macros are the big guns and as | such if you aim them wrong you are going to blow both feet off. | A declarative macro is only at worst going to spew nonsense | into your post-processed source where it was used - which the | Rust tools are quite happy to show you - but a sufficiently bad | proc macro could cause unlimited mayhem with no diagnostics | available. I don't know if somebody is collecting the worst | _mistakes_ they 've seen with proc macros, but m-ou-se's | nightly-crimes! shows what sort of insanity is possible. | | [ https://github.com/m-ou-se/nightly-crimes nightly-crimes! | blows away your compiler, running it again in a new environment | where it will allow nightly features even though you've got a | stable compiler installed... ] | akiselev wrote: | _> [ https://github.com/m-ou-se/nightly-crimes nightly- | crimes! blows away your compiler, running it again in a new | environment where it will allow nightly features even though | you've got a stable compiler installed... ]_ | | That macros have access to the entire language including | arbitrary IO is the defining feature of proc macros (not | without controversy). The insanity here is the Rust compiler | team adding the `RUSTC_BOOTSTRAP` env var which is a hack | used to bootstrap rustc using a stable compiler despite the | nightly features used by the codebase. | | All nightly-crimes does is use `std::process::Command` to | rerun the compiler with the variable set [1], which tells | rustc to throw all concepts of stability out the window. | | I haven't been following developments but one of the ideas | (even has a PoC iirc) was to distribute and run proc macros | as web assembly to improve build times and prevent such | shenanigans. | | [1] https://github.com/m-ou-se/nightly-crimes/blob/main/yolo- | rus... | ibraheemdev wrote: | Note that this version of the book has not been updated since | 2016. The updated version is here: | https://veykril.github.io/tlborm/introduction.html ___________________________________________________________________ (page generated 2021-11-21 23:00 UTC)