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, and it is exactly the src/bin/main.mach that mach init writes. It imports the print module and the runtime, declares an entry point, prints a line, and returns a zero exit code.
use std.print;
use std.runtime;
#[symbol("main")]
fun main(argc: i64, argv: **u8) i64 {
print.println("Hello, World!");
ret 0;
}
The program relies on the standard library as a dependency. mach init scaffolds a project with std already declared and fetched, or clone the standalone Mach Sieve starting point.
Imports
Modules are named by full, project-rooted dotted paths: the project id, then the file's path under its source directory. A use binds the module under its last segment, so use std.print; makes its symbols reachable as print.println. The runtime line brings the standard runtime into the build. Nothing here calls it by name, but the program cannot link without it.
use std.print; # bound as print
use std.runtime; # bring the runtime into the build
The use alias: path form binds another name: after use out: std.print; the same function is out.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 function the runtime calls once it has started the process (the image itself is entered at the runtime's _start on linux). Decorators attach to the declaration that follows them.
#[symbol("main")]
fun main(argc: i64, argv: **u8) i64 { ... }
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
The same toolchain scaffolds, fetches dependencies, builds and links:
$ mach init hello
created hello
realized std a78d528f4b89 branch/dev
$ cd hello
$ mach build .
built hello linux-x86_64 debug
./out/linux-x86_64/debug/bin/hello 51 KiB 51 modules 1.4s
$ mach run .
Hello, World!
mach init scaffolds hello/ with src/bin/main.mach and checks std out, so there is no separate fetch step. mach build compiles and links, and the binary lands at out/<target>/<profile>/bin/hello. mach run runs what the build wrote and never builds, so build again after an edit. 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