Advanced

Hello World in Carbon

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

Carbon is Google’s experimental successor to C++. The language is still pre-0.1, but the nightly toolchain now supports string printing via Core.PrintStr, making Hello World straightforward.

The Code

Create a file named hello.carbon:

import Core library "io";

fn Run() {
  Core.PrintStr("Hello, World!\n");
}

Understanding the Code

  • import Core library "io" - Imports the I/O library from Carbon’s Core package, which provides PrintStr, PrintChar, and Print.
  • fn Run() - The entry point for Carbon programs. The fn keyword declares a function. Run in the default library of the Main package is called when the program starts.
  • Core.PrintStr("Hello, World!\n") - Prints a string to standard output. PrintStr was added to the nightly toolchain in December 2025.
  • \n - A newline character at the end of the output.

Printing in Carbon

The Carbon toolchain’s nightly releases provide these I/O functions:

  • Core.PrintStr(str) - Prints a string to standard output (available since December 2025 nightlies)
  • Core.PrintChar(char) - Prints a single character to standard output
  • Core.Print(i32) - Prints a 32-bit integer to standard output

The language design envisions richer formatting (e.g., Core.Print("Hello, {0}!", name)), but this is not yet implemented in the nightly toolchain.

Running with Docker

Carbon’s nightly toolchain runs on Linux. We use an Ubuntu container that downloads and runs the toolchain:

1
2
3
4
5
# Pull the Ubuntu image
docker pull ubuntu:22.04

# Compile and run the program (downloads nightly toolchain inside container)
docker run --rm -v $(pwd):/app -w /app ubuntu:22.04 bash -c "apt-get update -qq && apt-get install -y -qq wget libgcc-11-dev > /dev/null 2>&1 && VERSION=0.0.0-0.nightly.2026.02.07 && wget -q https://github.com/carbon-language/carbon-lang/releases/download/v\${VERSION}/carbon_toolchain-\${VERSION}.tar.gz && tar -xzf carbon_toolchain-\${VERSION}.tar.gz && ./carbon_toolchain-\${VERSION}/bin/carbon compile --output=hello.o hello.carbon && ./carbon_toolchain-\${VERSION}/bin/carbon link --output=hello hello.o && ./hello"

Note: The first run downloads approximately 200 MB for the toolchain, so it will take a few minutes. Subsequent runs can reuse the cached Ubuntu image.

Running Locally (Linux Only)

If you’re on Ubuntu or a similar Linux distribution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# Download the nightly toolchain
VERSION="0.0.0-0.nightly.2026.02.07"
wget https://github.com/carbon-language/carbon-lang/releases/download/v${VERSION}/carbon_toolchain-${VERSION}.tar.gz
tar -xzf carbon_toolchain-${VERSION}.tar.gz

# Install linker dependencies
sudo apt install libgcc-11-dev

# Compile to object file
./carbon_toolchain-${VERSION}/bin/carbon compile --output=hello.o hello.carbon

# Link to executable
./carbon_toolchain-${VERSION}/bin/carbon link --output=hello hello.o

# Run
./hello

Try It in the Browser

The easiest way to experiment with Carbon is through Compiler Explorer, which runs the toolchain in the browser with no installation required.

Expected Output

Hello, World!

Key Concepts

  1. Carbon is compiled - Source code (.carbon) is compiled to object files, then linked to native executables via LLVM
  2. Run() is the entry point - The Run function in the default library of the Main package serves as the program’s entry point
  3. C++ interoperability - Carbon is designed for seamless bidirectional interop with C++
  4. Checked generics - Unlike C++ templates, Carbon’s generics are type-checked at definition time
  5. Experimental status - The language is pre-0.1; syntax and APIs are actively changing

Next Steps

Carbon is an ambitious experiment in language design. To follow its development, visit the Carbon GitHub repository or try examples at carbon.compiler-explorer.com.

Running Today

All examples can be run using Docker:

docker pull ubuntu:22.04
Last updated: