Rust Language Comments

Rust language comments for Rustaceans

Comments can be used to explain Rust code and make it more readable. Rust uses two forward slashes // to indicate a comment.

Comments are also useful to prevent code from running, which is helpful when testing or tinkering with code.

Rust language comments

Creating a Comment in Rust

Comments can be created using two forward slashes ‘//’:

// This is a comment in Rust

The double slash tells Rust to ignore the rest of the line.

We can use comments to prevent code from executing. This means that we don’t have to delete code even if we don’t want it to execute. This is a very useful feature, as it allows us to comment out code that we don’t want to run but might still be useful to us.

fn main() {
    // This is a comment
    println!("Hello, world!");

Comments can be inside or outside function bodies:

// This comment is outside the function body

fn main() {
    // This comment is inside the function body
    println!("Hello, world!");

Multi Line Comments in Rust

Rust does not have support for multi line comments.

Each line containing a comment must be started with a double forward slash:

// Rust requires us to comment out
// each line individually

End of Line Comments in Rust

Comments can be placed at the end of the line of code. The code before the comment will still run, but anything after the double slashes will be ignored:

fn main() {
    println!("Hello, world!"); // This comment is at the end of a line of code.