Comment
A comment is text the compiler skips. It is written for people — the next person to read the code, or you in three months — and good comments explain why the code is the way it is, which the code itself cannot say. Rux has three kinds, and this lesson's program uses all of them.
| Kind | Syntax | Used for |
|---|---|---|
| Line comment | // … to the end of the line | Everyday notes, and switching a line off |
| Block comment | /* … */, may span lines and nest | Longer notes, or a note inside a statement |
| Documentation comment | /// … or /** … */ before a declaration | Describing a function or type for rux doc |
Line comments
Two slashes start a comment that runs to the end of the line. It can have a line to itself or follow code:
// A comment can have a line of its own, or follow code on the same line.
PrintLine("Comments never reach the program."); // This one sits beside a statement.
Putting // in front of a line of code is the quickest way to switch it off without deleting it:
// PrintLine("This line never runs.");
Block comments — and why nesting matters
A block comment starts with /* and ends with */. It can cover several lines or sit in the middle of one, even inside a call:
PrintLine(/* the message */ "A block comment can sit inside a call.");
Unlike C, Java or JavaScript, Rux block comments nest: every /* needs its own */. That means you can wrap a block comment around code that already contains one, and it still works:
/* Block comments nest, which is less common than it sounds. …
/* an inner comment */
so this line is still inside the outer comment.
*/
flowchart LR
open1["/* outer opens"] --> open2["/* inner opens"] --> close2["*/ inner closes"] --> text["…still a comment…"] --> close1["*/ outer closes"]In a language without nesting, the first */ would end the whole comment and leave the rest as broken code.
Documentation comments
Three slashes start a documentation comment. It describes the declaration right after it, and tools read it: rux doc turns these comments into a package's reference pages.
/// A documentation comment starts with three slashes. It describes the declaration that follows
/// it — here `Main` — and tools read it: …
func Main() -> int {
/** … */ is the block form of the same thing. You will write many more of these in the Documentation lesson.
Comment markers inside text
Between double quotes, // and /* are just characters. The program prints them like any other text:
PrintLine("// is not a comment here");
PrintLine("/* and neither is this */");
The program
The whole lesson is one package in the Examples repository. Its comments explain every step.
// A comment is text the compiler skips. It is there for the people who read the code, and Rux has
// three kinds of it. This file uses all three.
//
// A line comment, like this one, starts with two slashes and runs to the end of the line. It is
// the everyday kind: use it to say why the code is the way it is.
import Io::PrintLine;
/* A block comment starts with a slash and a star and ends with a star and a slash. It can cover
several lines, or sit in the middle of one. */
/* Block comments nest, which is less common than it sounds. In many languages the first closing
star-slash below would end the whole comment and leave the line after it as broken code. In
Rux every opening pair needs its own closing pair:
/* an inner comment */
so this line is still inside the outer comment.
*/
/// A documentation comment starts with three slashes. It describes the declaration that follows
/// it — here `Main` — and tools read it: `rux doc` turns these comments into a package's
/// reference pages. `/** ... */` is the block form of the same thing.
func Main() -> int {
// A comment can have a line of its own, or follow code on the same line.
PrintLine("Comments never reach the program."); // This one sits beside a statement.
// A block comment can even sit inside a statement, between any two pieces of it.
PrintLine(/* the message */ "A block comment can sit inside a call.");
// Inside quotes, the comment markers are only characters, and they print like any others.
PrintLine("// is not a comment here");
PrintLine("/* and neither is this */");
// Putting `//` in front of a line switches it off without deleting it:
// PrintLine("This line never runs.");
// A documentation comment must document a declaration. Written inside a body, as in
//
// /// Prints the total.
// PrintLine("total");
//
// it is refused, because there is no declaration for it to describe:
//
// error: documentation comment inside a block is not attached to a declaration
//
// Inside a function, use `//`.
return 0;
}
Run it
cd Examples/Basics/Comment
rux run
Comments never reach the program.
A block comment can sit inside a call.
// is not a comment here
/* and neither is this */
Common mistakes
/// must sit directly before a declaration. Inside a function body there is none, and the compiler refuses it: error: documentation comment inside a block is not attached to a declaration. Inside a function, use //.Because block comments nest, a stray
/* inside a comment needs its own */. If the rest of your file suddenly seems to vanish, count the openers and closers.Try it yourself
- Comment out the second
PrintLinewith//and run again: one line fewer. - Wrap three lines of
Mainin a single/* … */, then add a nested/* … */inside it. Does the program still compile? - Move the
///comment abovefunc Mainso it sits inside the body instead, and read the compiler's message.
Learn more
- Comments in the Rux Reference
- Documentation — writing
///comments forrux doc