Skip to main content

Hello World

This guide covers writing and running your first Zynx program.


1. Create main.zx

import std.os;

fn main() {
os.write("Hello, world!\n");
}
  • import std.os;: Imports standard I/O.
  • fn main(): Program entry point.
  • os.write(...): Writes to stdout.

2. Run the Program

zynx run main.zx

Behind the scenes, zynx run transpiles the code to C, compiles it, and executes the binary.

To build a persistent binary:

zynx build main.zx

3. Variables and Interpolation

import std.os;

fn main() {
let name = "Zynx";
let message = "Hello, {name}!\n"; // String interpolation
os.write(message);
}

4. Functions

import std.os;

fn greet(name: str) {
os.write("Hello, {name}!\n");
}

fn main() {
greet("world");
}

5. Error Handling Preview

Zynx uses error unions (T!) and try/catch.

import std.os;

fn print_message(msg: str) -> void! {
try os.write(msg); // Propagate potential error
}

fn main() -> void! {
try print_message("Hello, world!\n");
}
  • -> void!: Indicates the function may return an error.
  • try: If the call fails, the error is returned to the caller.
  • When main returns void! and an error reaches it, the runtime prints a diagnostic to stderr and exits with code 1.

Next Steps