Basics · Lesson 1.2

Comment

Source
Write the three kinds of comment — //, /* */ and /// — and see that block comments nest.
You'll need: Hello, World

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.

KindSyntaxUsed for
Line comment// … to the end of the lineEveryday notes, and switching a line off
Block comment/* … */, may span lines and nestLonger notes, or a note inside a statement
Documentation comment/// … or /** … */ before a declarationDescribing 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.

Src/Main.rux
// 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

A documentation comment with nothing to document.
/// 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 //.
An unclosed block comment.
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

  1. Comment out the second PrintLine with // and run again: one line fewer.
  2. Wrap three lines of Main in a single /* … */, then add a nested /* … */ inside it. Does the program still compile?
  3. Move the /// comment above func Main so it sits inside the body instead, and read the compiler's message.

Learn more