Rust Tutorial
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

Rust - Comments



The Comments are added in programming code with the purpose of making the code easier to understand. It makes the code more readable and hence easier to update the code later. Comments are ignored by the compiler while running the code. In Rust, there are two ways of putting a comment.

  • Single line comment
  • Multi-line comment

Single line Comment

It starts with // and ends with the end of that line. Anything after // to the end of the line is a single line comment and will be ignored by the compiler.

Example:

The example below illustrates how to use single line comments in a Rust code. The compiler ignores the comments while compiling the code.

fn main() {
  // first line comment
  println!("Hello World!."); // second line comment
}

The output of the above code will be:

Hello World!.

Multi-line Comment

Rust does not have multi-line comment functionality however this can be achieved by starting each line by //.

Example:

In below code, multi-line comments are used which is achieved by starting each line by //.

fn main() {
  //comment line 1
  //comment line 2

  println!("Hello World!.");

  //comment line 3
  //comment line 4

  println!("Programming is fun.");
}

The output of the above code will be:

Hello World!.
Programming is fun.


5