2.16 Comments and Documentation
Rust, like C, supports //
for single-line comments and /* ... */
for multi-line comments. Additionally, Rust provides documentation comments processed by rustdoc
to generate HTML documentation.
2.16.1 Comments
#![allow(unused)] fn main() { // This is a single-line comment /* This is a multi-line comment */ }
2.16.2 Documentation Comments
///
documents the next item (for example, a function or struct).//!
documents the enclosing scope (for example, a module or crate).
#![allow(unused)] fn main() { //! This module provides basic mathematical operations for `i32` values. /// Adds two numbers. /// /// # Examples /// /// ``` /// let result = add(2, 3); /// assert_eq!(result, 5); /// ``` fn add(a: i32, b: i32) -> i32 { a + b } }
The rustdoc
tool extracts these comments to generate structured HTML documentation. Additionally, multi-line documentation comments starting with /**
or /*!
are supported, but they are less common.