Rust Comments
Comments are notes you leave in your code for humans to read. Rust completely ignores them when it compiles, so they never affect how your program runs. Good comments explain why code does something, not just what it does.
Line comments
Section titled “Line comments”The most common comment starts with //. Everything after // on that line is ignored:
fn main() { // This is a comment — Rust skips it entirely println!("Hello!"); // comments can also sit at the end of a line}Output:
Hello!Multi-line comments
Section titled “Multi-line comments”For a longer note, you can start every line with //:
fn main() { // This program greets the user. // It prints a single line of text. // Written as a first example. println!("Hello!");}Output:
Hello!Rust also supports block comments between /* and */, which can span several lines:
fn main() { /* This is a block comment. It can cover multiple lines without a // on each one. */ println!("Hello!");}Output:
Hello!In practice, most Rust code uses // even for multiple lines — it’s the community norm and works well with editor shortcuts.
Commenting out code
Section titled “Commenting out code”Comments are handy for temporarily disabling a line while you test something:
fn main() { println!("This runs"); // println!("This is switched off for now");}Output:
This runsWriting comments that help
Section titled “Writing comments that help”A comment that repeats the code adds nothing. The code already says what happens. A good comment says why, or warns about something the reader can’t see:
fn main() { let minutes = 135;
// The timer only shows whole hours. Integer division drops // the remainder, which is exactly what we want here. let hours = minutes / 60; let leftover = minutes - hours * 60; // minutes that didn't make a full hour
println!("{hours} h {leftover} min");}Output:
2 h 15 minCompare that with a comment like // divide minutes by 60. It’s true, but anyone can read minutes / 60. The useful information is why dividing is safe here.
A few more habits worth picking up:
- Keep comments up to date. A comment that no longer matches the code is worse than no comment, because people trust it.
// TODO:is a common way to mark unfinished work, like// TODO: handle negative numbers. Many editors highlight it and can list every TODO in a project.- Block comments nest in Rust. Unlike in C, you can wrap code that already contains a
/* */comment in another one, and it still works:
fn main() { /* Switched off for now: println!("old greeting"); /* said hi to everyone */ */ println!("new greeting");}Output:
new greetingDocumentation comments
Section titled “Documentation comments”Rust has a special kind of comment that starts with /// (three slashes). These are doc comments — they describe functions, and Rust can turn them into a browsable documentation website with cargo doc.
/// Adds two numbers together and returns the result.fn add(a: i32, b: i32) -> i32 { a + b}You don’t need doc comments yet as a beginner — just know that /// is for documentation, while // is for ordinary notes. When you’re ready, the Documentation page shows how to write them, build the docs, and even turn examples into tests.
Practice
Section titled “Practice”1. Switch off one line
Section titled “1. Switch off one line”This program prints three lines. Turn the middle line into a comment so only Line 1 and Line 3 are printed. Hint: put // at the start of the line you want to switch off.
fn main() { println!("Line 1"); println!("Line 2"); println!("Line 3");}Show solution
fn main() { println!("Line 1"); // println!("Line 2"); println!("Line 3");}Output:
Line 1Line 32. Switch off two lines at once
Section titled “2. Switch off two lines at once”Now print Line 1 to Line 4, but use a single block comment to switch off lines 2 and 3. Hint: start the comment with /* before line 2 and end it with */ after line 3.
Show solution
fn main() { println!("Line 1"); /* println!("Line 2"); println!("Line 3"); */ println!("Line 4");}Output:
Line 1Line 4- Comments are notes Rust ignores; use them to explain your code.
//starts a line comment (the most common kind)./* ... */is a block comment for multiple lines, and block comments can nest.- Good comments explain why, not what the code already says.
///is a doc comment used to generate documentation.
Quick check
Section titled “Quick check”Quick check
1. What does Rust do with comments when it compiles your code?
2. What are /// doc comments used for?
Score: 0 / 2
Read more
Section titled “Read more”- Comments — The Rust Book: the short official section on
//comments and where they usually go. - Comments — Rust by Example: every comment style in one runnable example, including block comments inside an expression.
- Making Useful Documentation Comments — The Rust Book: a first look at
///and//!and howcargo docuses them. - How to write documentation — The rustdoc book: advice on what good doc comments contain, for when you write a library.
- Comments — The Rust Reference: the exact rules, including nested block comments.