what are comments in Rust language? How do you write a comment in Rust?
Comments are description text about a line or block of a code.
Generally, Comments are for programmers not for Rust interpreters. At runtime, These are ignored by Rust.
In Rust, You can write a comment in different following ways
- Single-line comments
- Multi-Line comments
- Documentation commands
It is always good to practice to add Comments to the below blocks or lines of code
- Classes description
- before the method declaration
- Any complex logic code
- documentation comments
Single Line comments in Rust
These are also called inline comments.
- It always starts with double slashes (// ) symbol characters and ends with a line break.
- There is a space after a pound symbol
- a string of text that begins with a # symbol is ignored by the Rust compiler.
- It is a description or piece of text for a single line of code.
- These comments are not required at the starts in beginning of the line, but also can be written in the middle or end of the line Syntax:
// This is single line comments in the Rust file
Example
hello.rc:
// Hello World application
fn main() {
println!("Hello World app");
}
Learned single-line comments, How do you write multi-line comments
Multi-line comments
These comments are written in multiple lines and are also called block comments. Hello World program is the first step to learning any programming language Steps to write a Hello World First program code.
Here is a way we can write a multi-line comment with the #\ symbol. Each line starts with a #\ 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 multiple lines of code and is ignored by the compiler.
- Each line starts with a backslash(
//
) in multiple lines
Block level comments in Rust Example:
// Hello world sample program
// Basic program to print hello world to the 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
//
)