machdocs
Home GitHub

Hello world

A complete mach program in a handful of lines: a couple of imports, an entry point, and one printed line. Every piece is explicit, so nothing runs that you did not write.

The program

This is the whole thing. It imports the runtime and the print module, declares an entry point, prints a line, and returns a zero exit code.

use std.runtime;
use print: std.print;

#[symbol("main")]
fun main(argc: i64, argv: **u8) i64 {
    print.println("Hello, World!");
    ret 0;
}
Note

The examples rely on the standard library as a dependency. Run mach init to scaffold a project wired up to build, or clone the standalone Mach Sieve starting point.

Imports

Modules are named by full, project-rooted dotted paths. The first line pulls in the standard runtime that every program builds against; it takes no alias because nothing here calls it directly.

use std.runtime;          # bring the runtime into the build
use print: std.print;     # bind std.print to the short name print

The use alias: path form binds a short handle to a path. With print bound, the module's symbols are reached through it as print.println. See Modules for path resolution and aliases.

The entry point

Execution starts at a single function. Its name in source can be anything; what makes it the entry point is the exported symbol and its signature.

The symbol decorator

#[symbol("main")] exports the function under the symbol main, which is the name the program is entered through. Decorators attach to the declaration that follows them.

#[symbol("main")]
fun main(argc: i64, argv: **u8) i64 { ... }
Note

Decorators use the #[...] syntax. See Decorators for the full set.

Arguments and exit code

The entry function takes the argument count argc: i64 and the argument vector argv: **u8 (a pointer to an array of C-style strings). It returns an i64 process exit code, so ret 0; reports success.

Printing

print.println writes its argument followed by a newline. For formatted output, print.printf fills each {} hole in the format string with the next trailing argument, in order, and writes exactly what you give it.

print.printf("x = {}\n", 42::i64);   # x = 42

The ::i64 casts the literal to a concrete type. Because printf writes the string verbatim, include the trailing \n yourself.

Build and run

Build with the same single toolchain that vendors dependencies and links. From the project root:

mach init          # scaffold a new project
mach dep pull      # fetch dependencies (std)
mach build .       # compile to out/<target>/bin/

The compiled binary lands in out/<target>/bin/, where <target> is the selected target name. Run it directly from there. See the CLI reference for the full command set.

See also

  • Functions - declarations, parameters, and generics
  • Modules - dotted paths, use, and aliases
  • CLI - build, dependency, and test commands
  • Project layout - how a project tree maps to modules