Beginner Fundamentals

Comments

Comments explain code to humans; the compiler ignores them. Rust supports line comments, block comments, and special documentation comments.

Line comments

Use // for a comment that runs to the end of the line.

fn main() {
    // This is a line comment
    let x = 5; // comments can follow code too
    println!("{}", x);
}

Block comments

Use /* */ to comment across one or more lines.

fn main() {
    /*
      This is a block comment.
      It can span multiple lines.
    */
    println!("Done");
}

Documentation comments

Triple-slash /// comments document the item that follows them. Tools can turn these into HTML documentation.

/// Adds two numbers and returns the result.
///
/// # Examples
/// ```
/// let total = add(2, 3);
/// assert_eq!(total, 5);
/// ```
fn add(a: i32, b: i32) -> i32 {
    a + b
}

Use plain comments to explain tricky logic, and doc comments to describe the public API of your functions, structs, and modules.