4 ms·
Solving Advent of Code at compile time with Rust macros
- MeetingsBrowser 2y agoIs this really what Rust macros look like?
- C-programmer 2y agoYes, and I've seen them in production. Once you learn the rules, you can parse them reasonably well in your head but it is still really confusing at a glance.
- p4ul 2y agoThis is one kind of macro in Rust: declarative macros [1]. The other kind of macros in Rust are procedural macros [2]. Both are widely used in idiomatic Rust. And even within the procedural type of macros, there are different categories of macros (i.e., function-, or attribute-like macros). [1] https://doc.rust-lang.org/book/ch19-06-macros.html https://doc.rust-lang.org/book/ch19-06-macros.html [2] https://doc.rust-lang.org/book/ch19-06-macros.html#procedural-macros-for-generating-code-from-attributes https://doc.rust-lang.org/book/ch19-06-macros.html#procedura...
- CodeBrad 2y agoIt is! Although they are being used in a really weird and not normal way here. I found "The Little Book of Rust Macros"[1] to be a really good resource for getting started with Rust's declarative macros. [1]: https://veykril.github.io/tlborm/ https://veykril.github.io/tlborm/
- cinntaile 2y ago> Not only is the python solution barely legible The article then proceeds to produce barely legible code, but it's in Rust instead! I would leave out own-goals like this, it detracts from the content.
- deleted 2y ago[deleted]
- weebull 2y agoIt's full of stupid comments. E.g. the python implementation has this attached. ```py """Warning: This code completes in a shocking O(n log n) time complexity and allocates memory with reckless abandon""" ``` How shocking that sorting is an O(n log n) algorithm. Edit: he then goes on to use bubble sort. I think there might be a large amount of tounge in cheek with this blog post, but it doesn't really land.
- CodeBrad 2y agoTechnically, because the bubble sort is implemented at compile time the program is infinitely faster. The answer is produced as a compiler error and so there is zero runtime at all.
- amenhotep 2y agoI thought it all completely landed, that it was very obvious what you were doing, and that it was very funny as well as being real neato. Frankly I would probably put negative stock in the opinion of anyone who sees you accusing that python code of being hard to read and thinks you're remotely serious.
- CodeBrad 2y agoHey! I wrote this. The python being barely legible is my own fault, as the one who wrote it. The whole post is written to be a little ridiculous, but I must not have gotten that across =( I agree with you. In real life, python is absolutely a better choice than trying to write a Rust macro to produce the result as a compiler error. I added a footnote to the end of the referenced paragraph to say that python is great.