A comment is source text written for programmers rather than the computer. The compiler ignores it as program instructions, so comments can explain code, preserve reminders, or record why a choice was made. section 1.2
Rust provides three useful comment forms:
- `//` begins a comment that continues to the end of the line. section 1.2
- `/* ... */` surrounds a comment and can fit between code or span multiple lines. section 1.2
- `///` creates a documentation comment that Rust can turn into documentation for code. section 1.2
Explain a small program
In `fn main() { let some_number = 100; // This number is important }`, everything after `//` is ignored by the compiler. A longer explanation can use `/* ... */`, while `/// Converts a string slice in a given base to an integer.` can appear in generated documentation.
/// This is a struct that does nothing
pub struct DoesNothing {}
/// This struct only has one method.
pub struct PrintThing {}
impl PrintThing {
/// This function just prints a message.
pub fn prints_something() {
println!("I am printing something");
}
}
Browse documented structs and methods to see how `///` comments become descriptions in generated API documentation. section 18.4









