what are comments in Rust language? How do you write a comments in Rust?

Comments are description text about line or block of a code.

Generally Comments are for programmers not for Rust interpreter. At runtime, These are ignored by Rust.

In Rust, You can write a comments in different following ways

  • Single line comments
  • Multi Line comments
  • Documentation commands

It is always good practise to add Comments to the below blocks or lines of code

  • Classes description
  • before method declaration
  • Any complex logic code
  • documentation comments

Single Line comments in Rust

These also called inline comments.

  • It always starts with double slashes (// ) symbol character and ends with line break.
  • There is an space after a pound symbol
  • an string of text begins with # symbol are ignored by Rust compiler.
  • It is an description or piece of text for single line of code.
  • These comments are not required at starts in begin of line, but also can write in middle or end of line Syntax:
// This is single line comments in Rust file

Example

hello.rc

// Hello World application
fn main() {
    println!("Hello World app");
}

Learned single line comments, How do you write a multi line comments

Multi line comments

These comments written in multiple line and also called block comments. Hello World program is a first step to learn any programming language Steps to write a Hello World First program code.

Here is a way we can write a multi line comments with #\ symbol. Each line starts with #\ character.

// multi line comments 1
// multi line comments 2
// multi line comments 3

Rust provides special syntax to write a block level comments

// This comment is a multi line comment 1
// This comment is a multi line comment 2
// This comment is a multi line comment 3

This allows you to write a multiple lines of code and ignored by compiler.

  • Each line starts with backslash(//) in multiple lines

Block level comments in Rust Example:

// Hello world sample program
// Basic program to print hello world to console
// First code to write in Rust programming language
fn main() {
    println!("Hello World app");
}

Conclusion

You learned different types of comments in Rust

  • single line comments (//)
  • Multiple line comments (Each line begins with //)