Skip to main content
A block comment in Rust is a lexical construct used to instruct the compiler to ignore a specific sequence of characters during the lexical analysis phase. It is delimited by /* at the beginning and */ at the end, allowing the enclosed text to span multiple lines or be embedded directly within a single statement. Standard Syntax The lexer strips the comment and treats everything between the opening and closing delimiters as whitespace before parsing begins.
Inline Evaluation Because the Rust lexer evaluates block comments as whitespace, they can be placed inline within expressions without breaking the abstract syntax tree (AST).
Nesting Mechanics Unlike C and C++, Rust’s lexical analyzer explicitly supports nested block comments. The compiler maintains a depth counter for the delimiters; every opening /* found within an existing block comment requires a corresponding closing */ before the outermost comment is considered terminated. This prevents premature termination when commenting out blocks of code that already contain block comments.
Block Documentation Comments Rust differentiates standard block comments from block documentation comments via a specific character immediately following the opening delimiter. Instead of being entirely ignored, the compiler parses these into #[doc] attributes.
  • Outer block doc comment (/** ... */): Applies to the item immediately following it. Parses as #[doc = "..."].
  • Inner block doc comment (/*! ... */): Applies to the enclosing item (such as a module, crate, or function). Parses as #![doc = "..."].
Tired of Poor Rust Skills? Fix That With Deep Grasping!Learn More