3 ms·
Note 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 ord
by kibwen 6y ago
Note 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
- mprovost 6y agoIt's true that writing macros is out of scope for an intro guide, but the first "hello world" example uses the println! macro so you have to understand how to use them right from the start. They're all over the standard library. I struggled with explaining this when writing my own guide to Rust - do you dive straight into explaining macro syntax in the first chapter, or just explain how this one macro works and move on? Or don't explain it at all, which is the (sensible) choice that this tour takes. Unfortunately the language documentation isn't helpful either, looking at the docs for println! [0] doesn't explain the arguments that it takes (other than referring to format!, which is another macro [1]...) or what the return value of the macro is. [0] https://doc.rust-lang.org/std/macro.println.html https://doc.rust-lang.org/std/macro.println.html [1] https://doc.rust-lang.org/std/macro.format.html https://doc.rust-lang.org/std/macro.format.html
- pizza234 6y ago> the first "hello world" example uses the println! macro so you have to understand how to use them right from the start You don't "have to" understand the `println!` implementation in order to use it. It certainly helps like it does for any other API, but it can be treated as a blackbox, and still be able to use it.
- YesThatTom2 6y agoThis is the essence of teaching: planning out what to gloss over and explain later. I see many tech:nerd people explain this bottom-up instead of bottom-down because they can’t imagine skipping details for later. The reality is that students are intrigued by “I’ll explain what that means later” and it builds interest. I explain how our website works by first telling a big lie: your computer requests a webpage and we have one big computer that replied with the answer. I then explain that I just lied. I then break it down: a web page requires many elements. Each requested individually. I then explain we don’t have one big computer but a cluster behind a load balancer. I then explain the many benefits of a load balancer. Then I explain that the web cluster depends on databases and other services. All-in-all I expose 5-6 “lies” as I break down each component. I’ve gotten positive feedback from both non-developers, entry-level developers, and expert developers. I’ve also seen people give a bottom up description and everyone was confused at the end. Why do top-down descriptions work better even though they initially gloss over (lie) To the audience? Because the high level description gives The audience a mental roadmap to follow. Then we back up and walk through that roadmap in more detail. Then we walk through again in even more detail. The entire time the audience understands where we are going and fits each bit of new information into their mental model, expanding it as we go. Here’s how it feels to be explained to in a bottom-up manner: imagine I spent an hour with you listing out left and right turns. At the end of the hour I tell you “and that’s how You drive from NYC fo San Francisco”. JFC, Tom, couldn’t you have started with that!?!? Don’t be that guy. An explanation isn’t a murder-mystery novel with a surprise ending. Start with “these butler did it” and work backwards from there. At Bell Labs we had a saying about presentations: “first show us your conclusion slide. If we disagree, rewind to the first slide and show us your talk. You have our interest. If we agree, stop. We can all go to lunch early.”