Hello World in Hare
Your first Hare program - the classic Hello World example with Docker setup
Every programming journey starts with Hello World. Let’s write our first Hare program.
The Code
Create a file named hello.ha:
| |
Understanding the Code
use fmt;- Imports thefmtmodule from Hare’s standard library for formatted I/Oexport fn main() void =- Declares the program entry point;exportmakes it visible to the runtime,voidmeans it returns nothing{ ... };- The function body is an expression terminated with a semicolonfmt::println("Hello, World!")!- Prints the string followed by a newline!- The error assertion operator; it tells the compiler this I/O operation should not fail. If it does (e.g., stdout is closed), the program aborts with a trace
Running with Docker
The easiest way to run this without installing Hare locally is using Alpine Linux’s edge repository, which packages Hare:
| |
Note: Since there is no dedicated Hare Docker image on Docker Hub, we use Alpine Linux edge which includes Hare in its package repository. The apk add step installs the compiler before running your program.
Running Locally
Hare officially supports Linux, FreeBSD, OpenBSD, NetBSD, and DragonFlyBSD. If you have Hare installed:
| |
Expected Output
Hello, World!
Key Concepts
- Module imports - The
usekeyword imports standard library modules - Entry point -
export fn main() voidis the program’s starting point - Error handling - The
!operator is required when calling functions that may fail; it asserts success - Expression-based - Function bodies are expressions (note the
=after the return type and;after the closing brace) - QBE backend - Hare compiles through the QBE compiler backend, not LLVM
Understanding Error Handling
In Hare, I/O operations like fmt::println can potentially fail (e.g., if stdout is unavailable). The return type includes an error union, and Hare requires you to handle that possibility. The ! operator is one way to do it — it asserts the operation succeeds and aborts the program if it doesn’t:
| |
This is fundamental to Hare’s philosophy: every possible error must be acknowledged by the programmer.
The export Keyword
The export keyword on main is required because the Hare runtime needs to call your main function. Without export, the function would only be visible within its own module:
| |
File Extension
Hare source files use the .ha extension. The hare build driver handles compilation, linking, and execution through simple commands like hare run and hare build.
Next Steps
Continue to Variables and Data Types to explore Hare’s type system, including tagged unions, slices, and non-null pointers.