Comments in Rust
Comments let you leave notes in your code that the compiler completely ignores at runtime. Rust gives you three distinct types, each aimed at a different audience — yourself, your team, or automated documentation tools.
Types of Comments
Line Comments (//)
The most common comment in Rust. Everything from // to the end of the line is ignored by the compiler.
fn main() {
// price of one cutting chai in rupees
let chai_price = 30; // per cup, not per pot
// we order for two friends
let cups = 2;
// calculate subtotal using integer math
let subtotal = chai_price * cups; // 60
let total = subtotal + 5; // add 5 rupees for sugar extra
println!("Total: {total}");
}Total: 65A few things to notice:
| Usage | Example | Purpose |
|---|---|---|
| Comment above a line | // price of one cutting chai | Explains the intent of what follows |
| End-of-line comment | // per cup, not per pot | Adds a quick unit or reminder |
| Multi-line thought | Repeat // on each line | Rust style prefers this over block comments |
Rust style strongly favours // over /* */ for prose comments. Use // on consecutive lines for longer thoughts — it is easier to read and simpler to temporarily disable code.
Block Comments (/* ... */)
Rust supports C-style block comments. Unlike most languages, Rust block comments can be nested, which makes it safe to comment out code that already contains block comments.
fn main() {
let price = 100;
let shipping = 20;
/*
let discount = 10;
let coupon = 5;
let total = price + shipping - discount - coupon;
*/
let total = price + shipping;
println!("Total: {total}");
}Total: 120Nested Block Comments
fn main() {
/* outer comment
/* inner comment — valid in Rust! */
still inside outer */
println!("Block comments can be nested.");
}Even though block comments are valid, the Rust community convention is to use // for nearly everything. Reserve /* */ for quickly disabling a chunk of code during debugging, then remove it before committing.
Documentation Comments (///)
Adding one extra slash turns a comment into documentation comment. Write it directly above a function, struct, enum, or trait, and cargo doc will turn it into a formatted HTML page.
/// Returns the temperature in Fahrenheit.
///
/// Takes a Celsius value and applies the formula `F = C × 9/5 + 32`.
///
/// # Example
/// ```
/// let f = c_to_f(0);
/// assert_eq!(f, 32);
/// ```
fn c_to_f(celsius: i32) -> i32 {
celsius * 9 / 5 + 32
}
fn main() {
println!("{}", c_to_f(100)); // 212
}212Common /// Sections
| Section header | Purpose |
|---|---|
| (opening paragraph) | One-line summary of what the item does |
# Arguments | Describe each parameter |
# Returns | Describe the return value |
# Example | A runnable code snippet |
# Panics | Conditions under which the function panics |
# Errors | Error variants returned in a Result |
Code blocks inside # Example are run as tests when you execute cargo test. If your example compiles but produces a wrong result, the test will fail — keeping docs honest.
Module-Level Comments (//!)
Use //! (note the !) at the top of a file to document the file or crate itself, rather than the item that follows. These appear on the front page of your generated docs.
//! # chai_calc
//! A tiny practice crate to learn Rust comments and arithmetic.
//!
//! This comment will appear on the front page of the generated docs.
/// Adds two integers and returns the result.
fn add(a: i32, b: i32) -> i32 {
a + b
}
fn main() {
println!("{}", add(3, 4)); // 7
}7| Comment type | Written as | Documents |
|---|---|---|
| Item doc comment | /// | The function, struct, or enum immediately below |
| Inner doc comment | //! | The enclosing module or crate file |
Comment Styles at a Glance
Good Comment Habits
| Habit | Example |
|---|---|
| Write why, not what | // 5% tax is useful — // divide by 20 is not |
| Keep comments accurate | An outdated comment is worse than no comment |
Use TODO / FIXME markers | // TODO: add discount logic — editors highlight these |
| Remove dead code instead of commenting it out long-term | Commented-out code ages badly and confuses readers |
Rust does not treat TODO: or FIXME: as special keywords, but most editors and linters surface them automatically. They are a lightweight way to mark known gaps without creating a separate ticket.
Summary
| Type | Syntax | Audience | Visible in Docs? |
|---|---|---|---|
| Line comment | // | You and your team | No |
| Block comment | /* ... */ | You and your team (temp use) | No |
| Item doc comment | /// | API consumers via rustdoc | Yes |
| Crate/module doc comment | //! | API consumers via rustdoc | Yes (front page) |
What’s Next?
Now that you know how to annotate your code, you are ready to explore Control Flow — where you will learn how if expressions, loops, and match control the path your program takes at runtime.