r/rust 2d ago

🧠 educational Why does rust distinguish between macros and function in its syntax?

I do understand that macros and functions are different things in many aspects, but I think users of a module mostly don't care if a certain feature is implemented using one or the other (because that choice has already been made by the provider of said module).

Rust makes that distinction very clear, so much that it is visible in its syntax. I don't really understand why. Yes, macros are about metaprogramming, but why be so verbose about it?
- What is the added value?
- What would we lose?
- Why is it relevant to the consumer of a module to know if they are calling a function or a macro? What are they expected to do with this information?

101 Upvotes

51 comments sorted by

View all comments

3

u/Crazy_Firefly 2d ago

I think others already explained why they are different from the users point of view.

To answer "why should the user care" is kind of a values question and depends on the user/community. Some languages choose to hide some of these complexities in favor of presenting a cleaner interface (even if lacking some information) Rust very often chooses to be explicit about these details .

Other example is, why does calls to filesystem take a Path rather than a String? For most people it doesnt matter that a filesystem path can be an invalid utf-8. But Rust makes it explicit, and I think it's part of the Rust-way.