Guide
Comments and Assertions

Comments and Assertions

Comments

Comments are ignored by the compiler. They are used to add notes to the code to explain what it does, document it, or temporarily disable it.

There are three types of comments:

Single-line comments

Single line comments start with // and continue until the end of the line.

// single-line comment
1 + 1 // this becomes 2

Multi-line comments

Multi-line comments start with /* and end with */. They can span multiple lines.

/*
  multi-line comment
  this spans multiple lines
*/

They can also be inlined with code:

1 + /* this will become 2 */ 1

Documentation comments

Documentation comments start with /// (for "outer") or //! (for "inner"). When documentation is generated, the compiler will look for these comments and use them to generate documentation.

//! This describes the module.
 
/// This describes the function `foo`.
func foo() {}
⚠️

Comments do not work in string literals. The following are not comments:

"/* this is not a comment */"
"// neither is this"

Instead, the comments are interpreted as part of the string or character literal.

Assertions

Throughout this guide, we will use assertions to show the expected output of a piece of code.

Assertions call the following built-in functions (more on functions later in the guide):

  • assert(value)assert(value) - asserts that value is true.
  • assert_eq(left, right)assert_eq(left, right) - asserts that left is equal to right.
  • assert_ne(left, right)assert_ne(left, right) - asserts that left is not equal to right.

At runtime, if these assertions fail, the program will panic and exit with an error.