2.15 Comments and Documentation

2.15.1 Comments

  • Single-line comments use //.
// This is a comment
fn main() {
    // Another comment
    println!("Comments are ignored by the compiler");
}
  • Multi-line comments use /* */.
/*
This is a
multi-line comment
*/
fn main() {
    println!("Multi-line comments are useful");
}

2.15.2 Documentation Comments

  • Use /// for documentation comments that can be processed by tools like rustdoc.
/// Adds two numbers together.
///
/// # Examples
///
/// ```
/// let result = add(2, 3);
/// assert_eq!(result, 5);
/// ```
fn add(a: i32, b: i32) -> i32 {
    a + b
}

fn main() {
    let sum = add(2, 3);
    println!("Sum is: {}", sum);
}

2.15.3 Comparison with C

  • C uses // and /* */ for comments.
  • Documentation is often less standardized in C, though tools like Doxygen can be used.