4 ms·
> In contrast, you can’t really parse a file in Rust. Rust macros generate new source code, so parsing can’t be finished until all the macros are expanded. Rus
by zarzavat 8mo ago
> In contrast, you can’t really parse a file in Rust. Rust macros generate new source code, so parsing can’t be finished until all the macros are expanded.
Rust needs macros because the language is extremely strict and verbose. Macros are the main escape hatch, allowing you to simplify your code while maintaining memory safety without a GC.
Zig doesn't have memory safety as a goal so it seems like an unfair comparison.
- Philpax 8mo agoI'm a Rust main, but this argument seems... incorrect? You would not need macros for Rust to remain a usable memory-safe language. They certainly make it easier, but they're not necessary. It would be perfectly possible to design a variant of Rust that gets you to 80-90% of Rust's usability, with the same safety, without macros.
- kobebrookskC3 8mo agohow would you implement https://doc.rust-lang.org/stable/std/pin/macro.pin.html https://doc.rust-lang.org/stable/std/pin/macro.pin.html without macros? a macro is used to shadow the original variable so that you can't move it (safely) after you pin it
- steveklabnik 8mo agoRegular variable definition shadows. Macros expand to regular Rust code, they could always be replaced by the expanded body.
- kobebrookskC3 8mo agoyes, but the code inside is unsafe. the pin macro is like a safe function.
- steveklabnik 8mo agoI'm not sure what that has to do with anything. The macro isn't what makes it safe. The unsafe code being properly written is.
- kobebrookskC3 8mo agobut without macros, how would you expose a safe interface? fn pin(x: T) -> Pin<&mut T> { ... } would move the value
- Philpax 8mo agoYour macroless variant of Rust would offer a safe builtin that does this. It doesn't need to be implemented with a macro.
- jibal 8mo agoSince macros just expand into code, how could you imagine that a macro is ever necessary?
- kobebrookskC3 8mo agothe macro uses unsafe inside, so that's another instance of unsafe you'll need to check, whereas the pin macro is like a safe function
- jibal 8mo agoExcellent goalpost moving! Congratulations!
- kobebrookskC3 8mo agono, you just missed my point. expanding the implementation is not a safe abstraction. show me how you'd implement the functionality of the pin macro as a safe abstraction.
- jitl 8mo agoeverything in zig is unsafe and needs to be checked like rust unsafe, so…
- jibal 8mo agoI didn't miss that you totally changed the subject and now you're attacking a strawman. See Steve Klabnick's response to your other comment where you did this. Of course macros are good for encapsulation and abstraction, but that's a different subject--and note that the discussion was about Zig vs. Rust, and Zig has no macros so there's unencapsulated unsafe code all over the place. I won't respond further.
- kobebrookskC3 8mo agoi was responding this claim > It would be perfectly possible to design a variant of Rust that gets you to 80-90% of Rust's usability, with the same safety, without macros. i then present an api that i think relies on macros to expose a safe api > Of course macros are good for encapsulation and abstraction, but that's a different subject. no it's not. exposing safe abstractions is pretty much rust's raison d'être
- foltik 8mo agoMacros in Rust have nothing to do with memory safety. They're typically used for compile-time codegen (derive, println!) and very occasionally for syntactic abstraction (vec!).
- jibal 8mo agoThis makes no sense at all. The "comparison" (it isn't one--it's a technical point; "fairness" is not remotely relevant--Rust isn't being attacked and doesn't need defending) is between a language with macros and a language without macros--why the languages have or don't have macros isn't relevant. Zig is strict and verbose and could benefit from macros but for numerous reasons doesn't have them. Zig also has no GC and does have memory safety as a goal (but not the primary one, and not provable memory safety)--but none of this is at all relevant to the point the OP made, which is strictly about one language having macros and the other language not having them.
- armchairhacker 8mo agoA lot of the abstraction that Rust’s macros enable can be implemented via generics and traits, especially if they were extended (ex: keyword generics, variadic generics, orphan trait instances). Except traits are the other thing that complicates Rust’s name resolution…