Rust Comments
Comments are notes for humans. The compiler skips them completely, so they never change what your program does. Good comments explain why code exists or warn about something surprising; they make code easier to read for others — and for you, six months from now.
Single-line comments
A comment starts with two slashes // and continues to the end of the line. It can sit on its own line or after some code:
fn main() {
// Show a welcome message
println!("Welcome!");
let seats = 120; // total seats in the theatre
println!("Seats available: {}", seats);
}Welcome!
Seats available: 120
Multi-line comments
For longer notes you can write several // lines, or use a block comment that starts with /* and ends with */:
fn main() {
/*
This program prints a short countdown.
It is only three lines long, but block
comments can span as many lines as you like.
*/
println!("3");
println!("2");
println!("1");
}3
2
1
Block comments can even go in the middle of a line, although this is rarely a good idea:
let total = 10 /* base price */ + 2 /* tax */;Note: Unlike in C, Rust block comments can be nested:
/* outer /* inner */ still a comment */is valid. Most Rust programmers still prefer//for everyday comments.
Commenting out code
A common trick while experimenting is to put // in front of a line so it stops running, without deleting it:
fn main() {
println!("Step 1: open the box");
// println!("Step 2: shake the box");
println!("Step 3: close the box");
}Step 1: open the box
Step 3: close the box
Most editors comment or uncomment the selected lines with Ctrl+/ (or Cmd+/ on macOS).
Documentation comments
Rust has special comments that become documentation. A comment with three slashes /// describes the item directly below it, such as a function. The cargo doc command turns these comments into a website, and the Markdown inside them is rendered nicely.
/// Returns the area of a rectangle.
///
/// Both sides are measured in centimetres.
fn area(width: u32, height: u32) -> u32 {
width * height
}
fn main() {
println!("Area: {} cm²", area(4, 6));
}Area: 24 cm²
There is also //!, which documents the enclosing item. It is usually placed at the very top of a file to describe the whole module or crate.
Summary of comment styles
| Syntax | Kind | Typical use |
|---|---|---|
// text |
Line comment | Everyday notes and explanations |
/* text */ |
Block comment | Temporarily disabling a chunk of code |
/// text |
Outer doc comment | Documenting the next function, struct, enum … |
//! text |
Inner doc comment | Documenting the module or crate it is in |
Writing useful comments
- Explain the why, not the what.
// add 1 to inext toi += 1adds nothing, but// skip the header rowdoes. - Keep comments up to date. A wrong comment is worse than no comment.
- Prefer clear variable and function names; they often make a comment unnecessary.
Test yourself
Which syntax starts a single-line comment in Rust?
- #
- //
- --
- <!--
What is special about /// comments?
- They are ignored even by tools
- They are documentation comments used by
cargo doc - They make code run faster
- They can only be used in
main
Can Rust block comments be nested, as in /* a /* b */ c */?
- Yes
- No, it is a compile error
- Only in doc comments
- Only inside functions