I am pretty new to system programming and rust. I was going through the Rust By Example and almost immediately (ok, immediately... heh) came across macros.
I get macros, I think, but I was left with "why?". Would a function not accomplish the same thing? Is a macro, because it expands to source instead of adding to the call graph, be faster?
I guess I am looking for a "why macros?" explanation.
In addition to the above, macros allow you to encapsulate patterns that simply can't be done using a function abstraction. For example, the try! macro conditionally returns from the enclosing function. There's no way you could put this in a function, since the return would have to be non-local. Other macros allow you to declare types, functions, etc... that match a template. In both cases, the pattern being expressed is incompatible with the abstraction that functions provide.
Why is assert a macro? There are multiple reasons but the clearest is this: stringify!($cond). This is doing magic that is not possible within the language itself. stringify! takes (more-or-less) the raw syntactic tokens and, at compile-time, produces their representation as a string literal. IOW stringify!(1 == 2) expands to the static string slice "1 == 2". This is what lets the assertion failure error message say what assertion failed.
Try to imagine how you would do that with a function:
fn assert(cond: bool) {
// How do we recover what the expression was??
}