I have a question about one of the macros in Rust, which uses the format #! , such as #![allow] . I know how to define other macros in Rust, but I don't know how to define or write macros in this format. Through AI, I learned that this is to specify the functions of the compiler and the role of external crates, but AI cannot provide code. I want to know if I can define macros in this format?
NO.it is #! format.
#![...] is an inner attribute which applies to the item or crate containing it.
Currently, only outer attributes
#[...] can be user-defined macros; this is mostly just a syntactic restriction, as you can substitute an outer attribute for an inner attribute — except in the whole-crate case where there is no outside position.
So, there is no way to define a custom inner attribute. But perhaps there is another angle. Can you describe what outcome you want to achieve, instead of the specific syntax you hope to do it with?
I'm just curious and I found a blind spot in my knowledge. I already know how to write procedural macros and declarative macros in Rust. However, I came across a strange symbol "#!" for macro invocation. I just want to write macros in this format to invoke them. So, I want to learn more about this and understand if this format of macros cannot be customized. Is it a convention set by the compiler to control its behavior?
Here a summary highlighting the difficulties why custom inner attributes are currently unstable:
I just want to write macros in this format to invoke them.
You cannot currently create a macro that defines an inner attribute, that is, that can be used as
#![macro] rather than
#[macro]. But as per @jofas's link, this prohibition is not an intentional feature of the language —
#! is not “reserved” for anything — but rather something we haven't figured out how to do properly yet.
All of the attributes like
allow that can be used with
#![...] are not attribute macros, they are built-in attributes. From Attributes - The Rust Reference :
Attributes can be classified into the following kinds:
You can look at the built-in attribute list and understand which attributes aren't macros and instead are handled specifically by the compiler.