Carbon Playground

The Carbon Playground compiles and runs Carbon programs in your browser, with nothing to install. It runs the latest nightly build of the Carbon toolchain, so you can try the language as it is being developed.

Open in Compiler Explorer ↗
Press Run or Ctrl+Enter to compile and run your code.

Your code runs in a fresh, isolated sandbox on the carbonlang.dev server with the nightly Carbon toolchain, and is not stored there. It is saved in this browser so you can come back to it.

How to use the Carbon Playground

  1. Write Carbon code in the editor, or pick a program from the Examples menu.
  2. Press Run, or Ctrl+Enter (⌘+Enter on Mac), to compile and run it.
  3. Read the program output and any compiler errors in the output panel, then press Share to copy a link to your code.

Example Carbon programs

Load these from the Examples menu. They all compile and run on the toolchain the playground uses, so they show Carbon features that work today.

  • Hello world (C++ interop): imports the C++ <iostream> header and prints text with std::cout.
  • Fibonacci: a recursive function, if, and a for loop over Core.Range, printing with Core.Print.
  • Classes and methods: a class with a factory function, and methods that read the object (self) or change it (ref self).
  • Interfaces and generics: two classes that implement an interface, and a generic function that accepts any type implementing it.
  • Prime sieve: an array field, returned var, and a while loop; it returns the number of primes below 1000 (168) as its exit code.

Ways to run Carbon code

OptionInstall neededBest for
Carbon PlaygroundNoRunning small programs and sharing them as links
Compiler ExplorerNoInspecting the generated assembly
Nightly toolchainYes (Linux)Compiling and linking programs locally with the carbon command-line tool

Frequently asked questions

Is the Carbon Playground free?

Yes. It is free, needs no account, and runs in any modern browser with nothing to install.

Is there an online compiler for Carbon?

Yes. This playground compiles and runs Carbon code online with the official nightly Carbon toolchain. Compiler Explorer also has Carbon compilers and is the better choice for reading the generated assembly.

Which version of Carbon does the playground use?

The latest nightly build of the Carbon toolchain from the carbon-lang releases on GitHub. Each day the playground switches to the new nightly once all example programs compile and run on it. Carbon is experimental, so a program that works today may need changes as the language evolves.

Where does my code run?

On the carbonlang.dev server, in a fresh, isolated sandbox for each run: a gVisor container with no network access. Nothing is kept after the run: your code is not stored or logged, and a share link holds the code itself, encoded in the URL.

What are the limits of the Carbon Playground?

Each run gets 20 seconds to compile, 20 seconds to link, and 10 seconds to run, with 256 MiB of memory for the program, 1 CPU core, and empty standard input. Programs can be up to 64 KB; compiler messages, standard output, and standard error are each cut off at 64 KB. Each visitor can start 20 runs a minute, one at a time.

How do I print output in Carbon?

Add import Core library "io"; and call Core.Print(n) to print an integer on its own line. To print text, call C++ directly: add import Cpp library "<iostream>"; and write Cpp.std.cout << "Hello\n";.

What is the entry point of a Carbon program?

The toolchain starts the program at fn Run(), in a file with no package line. Declare it as fn Run() -> i32 to return the exit code.

Why does my Carbon code fail to compile?

The Carbon toolchain is an early, experimental compiler that implements only part of the language design described in these docs. Compiler errors appear in the output panel with line and column numbers.

Why does older or AI-generated Carbon code fail to compile?

Many Carbon examples online, and much AI-generated Carbon code, follow older versions of the language: they start with package Sample api;, define fn Main() -> i32, and print with Print("..."). The current toolchain rejects all three. Leave out the package line, start the program at fn Run(), and print integers with Core.Print and text with C++ std::cout.

Learn Carbon