In Rust programming, a comment is a programmer-readable explanation or annotation in the source code of a computer program. Comments are statements that are not executed by the compiler and interpreter. To write comments in Rust, we have two types of comments as in any other programming language.
- Non-doc Comments
- Single-Line Comments
- MultiLine Comments
- Doc Comments
- Outer doc comments
- Inner doc comments
Single-Line Comments:
To write single-lined comments we can use the // characters before the text you want to ignore while compiling and running.
Example 1:
Rust
fn main() {
println!( "Hello, world!" );
}
|
Output:
As we can the code inside the comment was not complied with and hence didn’t generate any error.
Multi-line comments:
Using multi-line comments we can ignore multiple lines in the source code file. We begin the comment with /* and end the comment with */
Example 2:
Rust
fn main() {
println!( "Hello, world!" );
println!( "Hello, Geeks!" )
}
|
Output:
Doc comments:
Rust also has doc comments which can be used to document a particular entity in the source code, that can be a function, struct, or any other entity. We can use as follows:
- Outer doc comments
- Inner doc comments
Outer Doc comments:
We use outer doc comments to document some code outside the block. It helps in knowing the high-level overview of the entity in the code.
Example 3:
Rust
fn main() {
println!( "Hello, Geeks!" )
}
|
Output:
Inner Doc comments:
The inner documentation comment can be used to get fine-grain documentation inside the block of code. This can be used with //!
Example 4:
Rust
fn main() {
greet()
}
fn greet(){
println!( "Hello, Geeks!" )
}
|
Output:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!