An inner attribute is a metadata directive that applies to the enclosing lexical scope or Abstract Syntax Tree (AST) node in which it is declared, rather than the item immediately following it. It is syntactically distinguished by an exclamation mark (Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
!) placed between the hash symbol (#) and the opening bracket ([).
Syntax
Inner attributes follow the same structural forms as outer attributes—simple, key-value, or list—but utilize the#![...] token sequence.
Placement and Parsing Rules
Because an inner attribute modifies the scope that contains it, the Rust compiler enforces strict parsing rules regarding its placement:- Valid Scopes: Inner attributes are permitted at the crate root (e.g.,
main.rsorlib.rs), and inside modules, function or expression blocks,implblocks, andexternblocks (foreign modules). - Top of Scope: They must appear at the very beginning of their enclosing scope. They can only be preceded by comments, whitespace, or other inner attributes.
- Invalidation: If an inner attribute is placed after any standard item declaration (such as a
fn,struct, oruseitem) or after any statement (such as aletbinding or an expression statement) within the same scope, the compiler will emit a syntax error (an inner attribute is not permitted in this context).
AST Mechanics: Inner vs. Outer
The distinction between inner and outer attributes is purely mechanical in how the compiler constructs the AST. In the Rust AST, attributes are not standalone sibling nodes; they are parsed as properties (specifically, theattrs field) of an AST node.
- Inner Attribute (
#![...]): Appended to theattrsfield of the AST node representing the enclosing scope (the parent node). - Outer Attribute (
#[...]): Appended to theattrsfield of the AST node representing the item immediately following the attribute.
Block-Level Application
Inner attributes can be applied to arbitrary expression blocks and function bodies. The attribute modifies the AST node of the block expression itself.Master Rust with Deep Grasping Methodology!Learn More





