Hello, World
Every programming course starts by printing one line, and for good reason: it proves the whole toolchain works — the compiler, the standard library and your terminal — before you write anything harder. This lesson's program is only four lines, but each one shows something you will use in every Rux program you write.
Anatomy of a program
import Io::PrintLine;
func Main() -> int {
PrintLine("Hello, World!");
return 0;
}
| Line | What it does |
|---|---|
import Io::PrintLine; | Brings the name PrintLine from the standard Io package into this file. |
func Main() -> int { | Declares the function Main, where every program starts. It returns an int. |
PrintLine("Hello, World!"); | Calls PrintLine with one piece of text. It prints the text and ends the line. |
return 0; | Ends Main and hands 0 back to the operating system: success. |
Statements end with a semicolon, and a function's body sits between braces.
Imports and dependencies
PrintLine is not built into the language. It lives in Io, one of the standard packages that ship with the compiler. Two things make it usable:
- The package's manifest,
Rux.toml, listsIounder[Dependencies]:[Dependencies] Io = { Namespace = "Rux", Version = "*" } - The source file imports the name it needs with
import Io::PrintLine;. The::separates the package from the name inside it.
Together they keep every dependency visible: the manifest says which packages a program uses, and each file says which names it takes from them.
Main and the exit status
When the program starts, the operating system calls Main. When Main returns, the number it returns becomes the program's exit status — the value scripts and other programs see. By convention 0 means the program succeeded and anything else means it failed.
sequenceDiagram
participant OS as Operating system
participant Main
participant Io as Io::PrintLine
OS->>Main: start the program
Main->>Io: PrintLine("Hello, World!")
Io-->>OS: "Hello, World!" on the console
Main-->>OS: return 0 (success)You can see the exit status yourself. Run the program, then ask the shell for it — echo $? in Bash or zsh, $LASTEXITCODE in PowerShell. Change return 0; to return 3;, run again, and the shell reports 3.
The program
The whole lesson is one package in the Examples repository. Its comments explain every step.
// The smallest complete Rux program: an import, an entry point and one line of output. Running
// `rux run` in this directory builds the program and then runs it.
//
// `import` brings a name from another package into this file. `PrintLine` lives in `Io`, the
// standard input and output package, and the `[Dependencies]` table in Rux.toml is what makes
// `Io` available to import from.
import Io::PrintLine;
// `Main` is where every program starts. The `int` it returns is the exit status, and zero means
// the program finished successfully.
func Main() -> int {
// `PrintLine` writes the text in the quotes and then ends the line.
PrintLine("Hello, World!");
return 0;
}
Run it
cd Examples/Basics/Hello
rux run
Hello, World!
Common mistakes
Remove the
Io = … line from Rux.toml and the import has nothing to import from. The compiler says the package Io is not listed in [Dependencies] and suggests adding it.PrintLine("Hello, World!") with no ; makes the compiler stop at the next line: error: expected ';' after expression, but found 'return'. The error points at the token after the gap, so look one line up.Try it yourself
- Print your own name on a second line, with a second
PrintLinecall. - Make
Mainreturn1, run the program and read the exit status in your shell. - Change
import Io::PrintLine;toimport Io::Print;and replace both calls withPrint. What happens to the line breaks? The Console lesson explains it.
Learn more
- The
Mainfunction in the Rux Reference - Imports and modules
- Your first project — create this package yourself with
rux new