-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Guide: explain why things like println!
and vec!
are macros in Rust
#17190
Comments
In C++, container initialization looks like It's very ugly to have a special cased macro for a type like String formatting has to use macros because Rust doesn't have CTFE and variadic generics. Rust doesn't have comparable metaprogramming capabilities to C++ and D at a type system level. Macros are used to work around language limitations, and although Rust's macros are less ugly and less crippled (explicit at the call site, hygienic, pattern matching on tokens or fully procedural) it isn't a good thing that the language needs macros for things like this. |
Yes, I feel like this goes better in the (eventually redone) macro guide than in the Guide. |
@steveklabnik: That's a good place for a more detailed explanation, but since most people will first read the guide, they will have these questions in the back of their minds. I think it would be good to at least point to the Macro guide. |
The Guide cannot and should not explain all details. |
Of course not. I mean something like |
Yes, |
Since this issue is high in the Google results for this question, could someone give a brief explanation of why |
It's mentioned that it will be discussed later on in the (advanced) section, but a link to that section would help. |
@oconnor663: Rust doesn't have compile-time function execution and variadic generics so it can't do the compile-time format string checking and code expansion without it. Macros are used to work around missing features, and in cases where they are very commonly used it would be better to improve the underlying language. |
@thestinger awesome, thanks. |
I know that this issue is over a year old, but since it's ranked quite high in Google, I felt like adding some different opinion can be helpful. From my point of view: Macros in Rust are not just intended to "work around missing features" -- they are pretty clever. Short: Rust-macros are expanded at compile time: I suggest invoking As far as I have seen, they are also used to introduce new features and shorthands on the syntax level. For me this seems like an elegant solution... How do other languages manage this? |
@duesee: I don't see it as elegant in the case of |
Yeah,
... but that pr didn't answer this issue's question -- Why Now I (Liigo, a rust newbie) will try to answer:
Feel free to correct me if I'm wrong. Thanks! Edit: no two years delay. |
@liigo: from my perspective you expressed the above comments a bit harshly. For example, ‘eventually did something’ will perhaps come across as if the author was just being lazy here. I'll of course assume you didn't mean to leave such a criticism since @steveklabnik has been doing a lot. 👍 About the heart of your comment though: I see your point, I totally feel what you're getting at. The remark about macros that @steveklabnik made there leaves me, you and I guess many enthusiastic readers rather unsatisfied. I like the simplicity of your rephrasing, especially the unparenthesized part (the first two sentences). One note: your English grammar wasn't fully correct yet. I think the basic improvement suggestion for @steveklabnik and other documentation authors would be: don't write something is just too advanced or will be addressed elsewhere without elaboration. |
@sanmai-NL I'm sorry for that! Updated. |
…_rpit, r=Veykril Fix: Lifetime's Bound Var Debrujin Index in Dyn Traits Surely fixes rust-lang#17182 I have tried running the analysis-stats in some of the repos mentioned in rust-lang#17080. No panic in almost all of them.
Many C/C++-people will wonder why in Rust macros are used for such simple tasks as printing and building a vector, while macros in C/C++ are mostly used for complex/advanced stuff.
Something like
in Rust this is a macro because ...
oryou could use the following Rust functions directly, but calling the macro is easier because ...
The text was updated successfully, but these errors were encountered: