- 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");
}
- 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);
}
- C uses
//
and /* */
for comments.
- Documentation is often less standardized in C, though tools like Doxygen can be used.