Rust Programming
A Comprehensive Guide to Effective Comments In Rust Programming
- Types of Comments in Rust Program:
- Line Comment in Rust Program:
- Block Comment in Rust Program:
- Use Comments To Disable parts of Our Code Temporarily:
Comments are easy for code readability, maintenance, and cooperation among developers. They explain the logic behind the code, relate algorithms, and provide insights into the programmer’s purpose. While it’s essential to write clear and self-explanatory code, comments can complement the code by offering additional context or explanations that might not be immediately apparent from the code itself. For Example-
// entry point of the program
fn main() {
// print text on the screen
println!("Hello, World!");
}
Here, // entry point of the program
and // print text on to the screen are comments
.
In Rust, the fn main()
function serves as the entry point for the program. When the program is executed, it starts running from the main
function.
Inside the main
function, there is a single statement:
println!("Hello, World!");
This line uses the println!
macro to print the text “Hello, World!” to the console. The println!
macro is a formatting macro in Rust Program, similar to println
in other languages, but with indicating that it’s a macro.
¶Types of Comments in Rust Program:
There are two important types of comments in Rust:
-
// This is a line comment
-
/* This is a block comment spanning multiple lines. */
¶Line Comment in Rust Program:
In Rust, you can use the double forward slash (//
) to create a line comment. something following the double forward slash on the same line will be treated as a comment and will not be executed by the Rust compiler. Here’s an example:
fn main () {
// declare a variable
let x = 1;
println!("x = {}", x);
}
¶Output :
x = 1
In the example above, the text following the double forward slash (//) is a comment, and it does not affect the execution of the program.
We can use the line comments in the same line as the code. For example-
fn main() {
let x = 1; // declare a variable
println!("x = {}", x);
}
¶Block Comment in Rust Program:
In Rust, we can use /* */
for block comments.
Here’s an example:
fn main() {
/*
declare a variable
and assign value to it
*/
let x = 1;
println!("x = {}", x);
}
¶Output:
x = 1
We can see the block comment extends for multiple lines. Hence, it is also known as multi-line comments.
We can also make multi-line comments using multiple line comments. For example-
fn main() {
// declare a variable
// and assign value to it
let x = 1;
println!("x = {}", x);
}
¶Output:
x = 1
Here, we have used two single-line comments: // declare a variable
And // and assign value to it
instead of a multi-line comment.
¶Use Comments To Disable parts of Our Code Temporarily:
Comments are also useful for temporarily disabling chunks of code Let’s see below an example:
fn main() {
let x = 1;
let y = 2;
let z = 3;
println!("z = {}", z);
}
This example of code will throw a warning because both x
and y
variables are unused.
fn main() {
/*
temporarily disable x and y variable declarations.
let x = 1;
let y = 2;
*/
let z = 3;
println!("z = {}", z);
}
¶Output
z = 3
Getting Started with Rust Programming For Beginner's Guide
A Guide to Optimizing Print Output in Your Rust Programming
All Tutorials in this playlist
Popular Tutorials
Categories
-
Artificial Intelligence (AI)
11
-
Bash Scripting
1
-
Bootstrap CSS
0
-
C Programming
14
-
C#
0
-
ChatGPT
1
-
Code Editor
2
-
Computer Engineering
3
-
CSS
28
-
Data Structure and Algorithm
18
-
Design Pattern in PHP
2
-
Design Patterns - Clean Code
1
-
E-Book
1
-
Git Commands
1
-
HTML
19
-
Interview Prepration
2
-
Java Programming
0
-
JavaScript
12
-
Laravel PHP Framework
37
-
Mysql
1
-
Node JS
1
-
Online Business
0
-
PHP
28
-
Programming
8
-
Python
12
-
React Js
19
-
React Native
1
-
Redux
2
-
Rust Programming
15
-
SEO - Search Engine Optimization
1
-
Tailwind CSS
1
-
Typescript
10
-
Uncategorized
0
-
Vue JS
1
-
Windows Operating system
1
-
Woocommerce
1
-
WordPress Development
2
Tags
- Artificial Intelligence (AI)
- Bash Scripting
- Business
- C
- C Programming
- C-sharp programming
- C++
- Code Editor
- Computer Engineering
- CSS
- Data Structure and Algorithm
- Database
- Design pattern
- Express JS
- git
- Git Commands
- github
- HTML
- Java
- JavaScript
- Laravel
- Mathematics
- MongoDB
- Mysql
- Node JS
- PHP
- Programming
- Python
- React Js
- Redux
- Rust Programming Language
- SEO
- TypeScript
- Vue JS
- Windows terminal
- Woocommerce
- WordPress
- WordPress Plugin Development