Beginner

Hello World in Roc

Your first Roc program - the classic Hello World example with Docker setup

Every programming journey starts with Hello World. Let’s write our first Roc program.

The Code

Create a file named main.roc:

app [main!] { cli: platform "https://github.com/roc-lang/basic-cli/releases/download/0.20.0/X73hGh05nNTkDHU06FHC0YfFaQB1pimX7gncRcao5mU.tar.br" }

import cli.Stdout

main! = |_args|
    Stdout.line!("Hello, World!")

Understanding the Code

  • app [main!] - Declares this as a Roc application that exposes the main! function. The ! indicates it performs side effects.
  • { cli: platform "..." } - Specifies the platform dependency. In Roc, a platform handles I/O and memory management while your application code stays purely functional.
  • import cli.Stdout - Imports the Stdout module from the cli platform for printing to the console.
  • main! = |_args| - Defines the main function using lambda syntax. The _args parameter (prefixed with _) indicates command-line arguments that are unused here.
  • Stdout.line!("Hello, World!") - Prints the text followed by a newline. The ! suffix marks this as an effectful function call.

Running with Docker

The easiest way to run this without installing Roc locally:

1
2
3
4
5
# Pull the official Roc nightly image
docker pull roclang/nightly-ubuntu-2204:latest

# Run the program
docker run --rm -v $(pwd):/app -w /app roclang/nightly-ubuntu-2204:latest roc main.roc

Note: On the first run, Roc will download the basic-cli platform specified in the source file. This may take a few seconds.

Running Locally

If you have Roc installed (download from roc-lang.org/install):

1
2
# Run directly
roc main.roc

Expected Output

Hello, World!

Key Concepts

  1. Roc is compiled - Source code (.roc) is compiled to native machine code or WebAssembly
  2. Platform/Application split - Platforms handle I/O and memory; applications are pure functional code
  3. Effects are explicit - Functions with side effects are marked with !
  4. Type inference - Roc infers all types automatically; no type annotations are required

Running Today

All examples can be run using Docker:

docker pull roclang/nightly-ubuntu-2204:latest
Last updated: