Skip to content

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.

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!
▶ Try it Yourself

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!
▶ Try it Yourself

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!
▶ Try it Yourself

In practice, most Rust code uses // even for multiple lines — it’s the community norm and works well with editor shortcuts.

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 runs
▶ Try it Yourself

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 min
▶ Try it Yourself

Compare 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 greeting
▶ Try it Yourself

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.

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 1
Line 3
▶ Try it Yourself

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 1
Line 4
▶ Try it Yourself
  • 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

1. What does Rust do with comments when it compiles your code?

2. What are /// doc comments used for?

Score: 0 / 2