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 providesPrintStr,PrintChar, andPrint.fn Run()- The entry point for Carbon programs. Thefnkeyword declares a function.Runin the default library of theMainpackage is called when the program starts.Core.PrintStr("Hello, World!\n")- Prints a string to standard output.PrintStrwas 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 outputCore.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:
| |
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:
| |
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
- Carbon is compiled - Source code (
.carbon) is compiled to object files, then linked to native executables via LLVM Run()is the entry point - TheRunfunction in the default library of theMainpackage serves as the program’s entry point- C++ interoperability - Carbon is designed for seamless bidirectional interop with C++
- Checked generics - Unlike C++ templates, Carbon’s generics are type-checked at definition time
- 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.