2.16 Comments and Documentation
Rust, like C, supports //
for single-line comments and /* ... */
for multi-line comments. Additionally, Rust provides documentation comments that can be processed by tools like 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
Rust supports special documentation comments:
///
documents the next item (e.g., a function or struct).//!
documents the enclosing scope, such as 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 are less commonly used.