Getting started

Install moon and build, test, and extend your first project.

moon is a statically typed, expression-oriented language. The moon command creates projects, runs programs, checks types, formats code, and builds executables.

This guide takes you from installation to a small multi-file project.

Install

On x86-64 Linux:

curl -fsSL https://raw.githubusercontent.com/moonlang-dev/moon-releases/main/install.sh | bash

On Windows PowerShell:

irm https://raw.githubusercontent.com/moonlang-dev/moon-releases/main/install.ps1 | iex

Open a new terminal after the installer finishes, then verify the command is available:

moon version

Hello, moon

Create hello.mn:

fn main() {
  println("Hello, moon!")
}

Run it:

$ moon run hello.mn
Hello, moon!

Use moon check hello.mn when you only want to check the program, or moon build -o hello hello.mn when you want to keep the executable.

Create a project

moon new creates a project directory. moon init creates the same files in the current directory.

moon new greeter
cd greeter

The new project contains a manifest and a source file:

greeter/
  moon.toml
  src/
    main.mn

Each .mn file is a module named after the file. Add src/farewell.mn:

pub fn goodbye(name: string) -> string {
  "Goodbye, $name."
}

Then use it from src/main.mn:

use farewell.goodbye

fn main() {
  println("Hello, moon!")
  println(goodbye("moon"))
}
$ moon run src/main.mn
Hello, moon!
Goodbye, moon.

Declarations are private by default. Add pub when another module should be able to import one.

Handle errors

Operations that can fail commonly return Result<T, Error>. Use ? to return an error to the caller, or match when you want to handle it locally.

fn first_line(path: string) -> Result<string, Error> {
  let text = std.fs.try_read_file(path)?
  ok(text.split("\n").first_or_none().unwrap_or(""))
}

fn main() {
  match first_line("message.txt") {
    ok(line) => println(line),
    err(error) => println("could not read file: ${error.message}"),
  }
}

Add a dependency

Add a package from a Git repository:

moon add https://github.com/example/greeter.git --version ^1.2

moon updates moon.toml and writes a checksummed moon.lock. Import a package by using its package name as the first part of the path:

use greeter.hello.greet

Use moon update to update dependencies and moon vendor to copy locked dependencies into the project for offline work.

Everyday commands

moon run file.mn           Check and run a program
moon check file.mn         Check without running
moon test                  Run project tests
moon fmt --write .         Format moon files
moon lint .                Show code-quality suggestions
moon build -o app file.mn  Build an executable
moon doc std strings       Read API documentation

Run moon <command> --help for the options accepted by a command.

Where next