Rust Comments
Commenting in Rust is similar to other languages (C, Java) and supports two types of comments:
Example
// This is the first type of comment
/* This is the second type of comment */
/*
* Multi-line comment
* Multi-line comment
* Multi-line comment
*/
Comments for Documentation
In Rust, using //
makes everything after it until the first newline a comment.
Under this rule, three forward slashes ///
are still valid as the start of a comment. Therefore, Rust uses /// as the beginning of documentation comments:
Example
/// Adds one to the number given.
///
/// # Examples
///
///
/// let x = add(1, 2); /// /// ```
fn add(a: i32, b: i32) -> i32 { return a + b; }
fn main() { println!("{}", add(2, 3)); } ```
The function add
in the program will have an elegant comment and can be displayed in the IDE:
Tip: Cargo has a cargo doc
feature that allows developers to convert documentation comments in the project into HTML documentation.