TL ; DR:
Comments are lines of code that Rust will intentionally ignore (not execute). They don't do anything.
You add a single line comment in Rust by typing
//followed by any text:// This is a comment! println!("This is not!");
Full lesson:
Comments are lines of code that Rust will intentionally ignore. They don't do anything.
They're just used to create notes for yourself and others about what the code does.
There are two types of code comments in Rust:
1. Single line comments:
We create single line comments by typing // and Rust will ignore (not execute) any text between // and the end of the line.
Single line comments automatically end at the next line of code:
// Next line will greet the user:
println!("Hello user!");
Output of this code:
Hello user!
We can also use single line comments at the end of a line to explain the code:
println!("Hello user!"); // This line greets the user
println!("This is not a comment!");
Output of this code:
Hello user!
This is not a comment!
2. Multi-line comments
We can also comment multiple lines of code using multi-line comments.
We type /* to begin the comment and type */ to end the comment:
/* This is a
multi-line comment
println!("This will not run");
Next line will greet the user:
*/
println!("Hello user!");
Output of this code:
Hello user!
Assignment
Follow the Coding Tutorial and let's write some comments!
Hint
Look at the examples above if you get stuck.
Free plan: 30 tutor messages. Premium is unlimited.