Comments
Comments in Rust are a way to make your code easy to understand by writing small explanations about your code, comments are ignored by the compiler, so they don't affect the execution of your code. They can also be used in documenting your code using the rustdoc
tool.
In Rust, comments can be written in two ways:
- Single-line comments: These comments start with
//
and continue until the end of the line. For example:
You can also write the comment after a line of code, like this:
- Multi-line comments: These comments start with
/*
and end with*/
. For example:
Rustdoc
Comments are also used by rustdoc
, the Rust documentation tool to generate documentation for your code. You can use comments to write documentation for your functions, structs, and other items in your code.
In order to generate documentation, instead of using //
, you should use ///
for single-line comments and /** */
for multi-line comments. For example:
Or using /** */
:
When you hover over the function name in your code editor, you should see the documentation comment displayed as a tooltip.
Function doc
You can also run rustdoc
on your code to generate documentation for your project. For example, to generate documentation for a file named lib.rs
, you can run:
Conclusion
In this lesson, we learned about comments in Rust. Comments are a way to write small explanations write besides your code that are ignored by the compiler. They can also be used in writing documentation for your code using the rustdoc
tool.
In the next lesson, we are going to learn about control flow in Rust.