Rust Lang

Rust Comments

Comments are an incredibly useful feature that allows developers to include short but descriptive documentation in a program.

The ability to add comments to your code can help to improve readability for you and other developers who might read and use your code.

How do Comments Work?

Comments are nothing special. They are just a set of text characters that describe how or what a piece of code is doing.

Comment uses special characters that make the compiler ignore them. Let us discuss how we can implement and use comments in our programs.

Rust Comments

There are three types of comments in Rust:

  1. Simple or Line comments
  2. Block Comments
  3. Doc comments

Let us discuss.

Simple Line Comments

Simple or Line comments are the type of comments that span a single line. They are mainly used to insert a short and descriptive message into a block of code.

In Rust, we can insert single-line comments using double forward slash characters. An example is as shown:

fn main() {

// single line comment

println!("Hello, world!");


As mentioned, a single-line comment spans a single line. As shown in the example, abovethe compiler ignores the line after8 the two forward slash characters.

Block Comments

The second type of comment is known as block comments. These are closely similar to simple

Sttart by opening the comment block with a forward slash character and an asterisk, as (/*).

After that, we can add the comment text and close the comment block with an asterisk and backlash as (*/).

An example of a block comment in Rust is as shown:

fn main() {


a block comment


mutliple lines


println!("Hello, world!");


The code above shows creating a block comment that spans multiple lines.

Doc Comments

Rust also provides us with doc comments. There are two main types of doc comments in Rust:

  1. Inner doc comments
  2. Outer doc comments.

Inner Doc Comments

Inner doc comments are denoted by a double forward slash and a bang as //!. They are written inside a code block are mainly used to generate documentation for a code inside a specified block.

An example is as shown:

fn main() {

//! documention

println!("Hello, world!");


The code above denotes an inner doc comment.

Outer Doc Comment

As the name suggests, an outer doc comment refers to a comment surrounding an outer block of code and acting as the documentation for that block.

Outer doc comments are denoted by triple forward slashes. An example is shown:

/// main function


fn main() {

println!("Hello, world!");



This article helps you understand various types of comments in Rust and how to create short and descriptive messages for your code blocks.

We hope you enjoyed the article, thanks!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list