Rust Comments


Rust Comments

In this tutorial, we will learn how to write comments in Rust programming language. We will go through the different types of comments and how to use them in a program.


What are Comments

Comments in Rust are non-executable statements that are used to annotate the code, provide explanations, and make it more readable. They are ignored by the Rust compiler during compilation.


Syntax

The syntax to define comments in Rust is:

// Single line comment

/* Multi-line comment
   continues here */


Single line comments

  1. Single line comments in Rust start with // and continue to the end of the line.
  2. They are used to add short explanations or notes within the code.

Rust Program

// Print Hello, World! to the console
fn main() {
    println!("Hello, World!");
}

Output

Hello, World!


Multiline comments

  1. Multiline comments in Rust start with /* and end with */.
  2. They can span multiple lines and are used for longer explanations or to comment out blocks of code.

Rust Program

/* Print Hello, World! to the console
   This is a multi-line comment */
fn main() {
    println!("Hello, World!");
}

Output

Hello, World!