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 themain!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 theStdoutmodule from thecliplatform for printing to the console.main! = |_args|- Defines the main function using lambda syntax. The_argsparameter (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:
| |
Note: On the first run, Roc will download the
basic-cliplatform specified in the source file. This may take a few seconds.
Running Locally
If you have Roc installed (download from roc-lang.org/install):
| |
Expected Output
Hello, World!
Key Concepts
- Roc is compiled - Source code (
.roc) is compiled to native machine code or WebAssembly - Platform/Application split - Platforms handle I/O and memory; applications are pure functional code
- Effects are explicit - Functions with side effects are marked with
! - 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:latestLast updated: