How to write and run Rust code in the browser with the Rust Playground
Short answer: Open play.rust-lang.org, paste your code and press Ctrl+Enter. On a Mac, Cmd+Enter works too. There’s nothing to install.
The playground compiles and runs each submission in a container, so nothing touches your machine, and the last thing you typed is kept in your browser’s local storage. Closing the tab doesn’t lose it.
Run something
Section titled “Run something”Paste this and press Ctrl+Enter:
fn main() { let numbers = [3, 1, 4, 1, 5, 9, 2, 6]; let total: i32 = numbers.iter().sum(); println!("sum = {total}"); println!("max = {}", numbers.iter().max().unwrap());}sum = 31max = 9The big button on the left doesn’t always say Run. It picks a command from the code you’ve written:
| Your code | The button says | Which is |
|---|---|---|
has fn main |
Run | cargo run |
has #[test]s and no main |
Test | cargo test |
| neither | Build | cargo build |
The small button beside it opens a menu headed “What do you want to do?”, where you can override that and pick Run, Build, Test, ASM, LLVM IR, MIR, HIR or Wasm yourself.
Reading the intermediate output
Section titled “Reading the intermediate output”ASM, LLVM IR and MIR are the three that teach you something. ASM is the x86-64 assembly, LLVM IR is LLVM’s intermediate representation, and MIR is Rust’s own control-flow intermediate representation. Switch Mode to Release before you look, or you’ll be reading unoptimised output that says little about what the compiler does to real code.
Two of the choices come with conditions, and the menu tells you: HIR switches you to the nightly channel, and Wasm changes the crate type to cdylib.
Channels and the Tools menu
Section titled “Channels and the Tools menu”The Channel menu moves between stable, beta and nightly. You need nightly for HIR, and for Tools → Expand macros, which shows what your macros actually expanded to. That’s worth seeing once, because macro expansion is where a lot of confusing errors come from. The same Tools menu has Rustfmt, which reformats the code, and Clippy, which lints it. Run both before you share a snippet with anyone.
Share creates a GitHub Gist and gives you back a URL that reloads the code with the same channel, mode and edition. To link a snippet from your own site, use the code query parameter and URL-encode the code.
What it won’t do
Section titled “What it won’t do”There’s no network access while your code is compiled or executed, so anything that calls an API fails. Memory, disk and total execution time are capped. The crate list is the top 100 crates by all-time downloads on crates.io, plus the Rust Cookbook crates and their dependencies, so a use of something obscure won’t resolve. Anything that needs to run for a long time belongs on your own machine.
- Cargo and project layout — editions, dependencies, and everything else around the compiler
- Compiler errors — how to read a message, then one page per error code