5 ms·
Unfortunately for me, they don't mention macros in this tour of rust. Even after reading the official book, I'm still not comfortable with macro and macro synta
by wdroz 6y ago
Unfortunately for me, they don't mention macros in this tour of rust. Even after reading the official book, I'm still not comfortable with macro and macro syntax. Do you have any recommendations?
- kd5bjo 6y agoThe Little Book of Rust Macros is a pretty good, but dense, overview of how macro_rules! can be used. (Procedural macros are a completely different beast). https://danielkeep.github.io/tlborm/book/index.html https://danielkeep.github.io/tlborm/book/index.html
- 65536 6y agoAlso, there are a lot of uses of macro_rules! in the Rust compiler and standard library repository. https://github.com/rust-lang/rust/search?q=macro_rules&type= https://github.com/rust-lang/rust/search?q=macro_rules&type= Looking at some of those and reading https://doc.rust-lang.org/rust-by-example/macros/syntax.html https://doc.rust-lang.org/rust-by-example/macros/syntax.html and https://doc.rust-lang.org/reference/macros-by-example.html https://doc.rust-lang.org/reference/macros-by-example.html can potentially be instructive.
- burfog 6y agoDepending on your preference for macros, run "info m4" or "info cpp" to read the documentation. You can use both. You can run them more than once, perhaps changing the source with a sed or perl script between passes.
- kd5bjo 6y agoNative Rust macros are generally preferred, because they prevent a few classes of error that can happen with the purely textual macros cpp and m4 use. In particular, they prevent unpaired delimiters from appearing in the macro expansion and name collisions between the macro implementation and the callsite.
- kibwen 6y agoNote that I wouldn't expect a beginner to Rust to care about writing macros at all. Macros are something that you might introduce to a project eventually in order to abstract over some project-specific boilerplate (and only as a last resort; there are usually better abstractions to reach for first). I say this because someone coming from a functional language might be betrayed by their instincts here: it is relatively rare for Rust users to write macros themselves, and macro-heavy Rust code would generally be regarded as peculiar/unidiomatic.
- zozbot234 6y agoI'm not sure that's the full story. You need macros in Rust for feature parity with, e.g. C++ templates with their metaprogramming capabilities, and C++ is definitely not a pure functional language.
- rapsey 6y agoFeature parity with C++ is not a goal and neither should it be.
- d_tr 6y agoI do not see anything in zozbot234's comment that implies that Rust should strive for feature parity with C++. Some features, however, such as being able to use integer values as generic parameters, are arguably pretty useful.
- swsieber 6y agoIndeed. I think that's referred to as "const generics" in rust venacular
- wwright 6y agoIt’s worth noting that C++ templates themselves are a purely functional language, and a Turing complete one at that
- 6y ago
- michalxnet 6y agoI recommend https://m.youtube.com/watch?v=q6paRBbLgNw https://m.youtube.com/watch?v=q6paRBbLgNw Jon Gjengset series Crust of Rust is amazing.