Skip to content

Rust Playground

11 min read

Try Rust in your browser without installing anything! The Rust Playground is a powerful online tool for experimenting, learning, and sharing code.


The Rust Playground (play.rust-lang.org) lets you write Rust code in your browser and compile/run it on Rust’s servers (in a sandbox, not in the browser itself). It’s perfect for:

  • Quick experiments
  • Learning Rust
  • Sharing code snippets
  • Testing ideas

Time required: 10-15 minutes to explore


TypeScript Playground:

Rust Playground:

  • Rust Playground
  • Compiles and runs your code on Rust’s servers in a sandbox (not in your browser)
  • Shows compiler errors
  • Can switch between editions
  • Can run tests
  • Can format code
  • Can share via URL

Similar tools:


URL: https://play.rust-lang.org/

When you open it, you see:

fn main() {
println!("Hello, world!");
}

Ready to run immediately! Click “Run” or press Ctrl+Enter.


  • Syntax highlighting
  • Auto-completion
  • Error underlining (as you type!)
  • Multi-cursor editing (Ctrl+Click)

Try this:

fn main() {
let x = 5;
println!("x = {}", x);
}

Click “Run” and see:

Compiling playground v0.0.1 (/playground)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.50s
Running `target/debug/playground`
x = 5
  • Shows compilation steps
  • Shows program output
  • Shows any errors or warnings

Create shareable link:

  1. Write your code
  2. Click “Share”
  3. Copy the URL
  4. Anyone with the URL can see and run your code

Example URL:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=abc123xyz

Compare to TypeScript Playground:

  • TypeScript encodes code in URL
  • Rust uses gist (cleaner URLs)

Edition:

  • 2015 (original Rust)
  • 2018 (modern features)
  • 2021 (widely used)
  • 2024 (latest stable edition, recommended for new code)

Mode:

  • Debug (fast compile, slow runtime)
  • Release (slow compile, fast runtime)

Channel:

  • Stable (most recent release)
  • Beta (next release preview)
  • Nightly (latest features, unstable)
[Run ▶] [Format] [Clippy] [Miri] [Tools ▼] [Config ▼] [Share] [Help]

Run - Compile and execute Format - Auto-format code (like cargo fmt) Clippy - Lint code (like cargo clippy) Miri - Advanced interpreter (for unsafe code) Tools - ASM, LLVM IR, MIR, macro expansion, etc. Config - Edition, mode, channel Share - Get shareable link Help - Keyboard shortcuts

Note: The exact button labels and grouping shift between Playground versions. The capabilities below are stable even if the menu names change.


Key Differences from TypeScript Playground

Section titled “Key Differences from TypeScript Playground”

TypeScript Playground:

let x = 5;
console.log(x);
// Transpiled to JavaScript, runs in browser JS engine

Rust Playground:

let x = 5;
println!("{}", x);
// Sent to Rust's servers, compiled to a native binary, run in a sandbox

Note: Unlike the TypeScript Playground (which transpiles and runs entirely in your browser), the Rust Playground sends your code to a backend, compiles it with a real rustc/cargo toolchain, and runs the resulting native binary in a sandboxed container. That’s why it needs a moment to respond and why it can report genuine compile and runtime errors.

Why it matters: Rust Playground shows real compile errors, not just type errors.

TypeScript:

  • Basic editor
  • Type checking
  • JS output

Rust:

  • Editor with completions
  • Compiler errors/warnings
  • Format code
  • Lint code
  • View assembly
  • Run tests
  • View macro expansions

Rust Playground can share:

  • Source code
  • Compiler output
  • Standard output
  • Entire configuration

Perfect for:

  • Asking questions on forums
  • Reporting bugs
  • Teaching examples

Problem:

use std::fs;
fn main() {
let content = fs::read_to_string("file.txt").unwrap();
println!("{}", content);
}

Error:

No such file or directory (os error 2)

Why: The playground is sandboxed, no file system access.

Solution: Use string literals instead:

fn main() {
let content = "This is my file content";
println!("{}", content);
}

Problem:

// This won't work!
let response = reqwest::get("https://api.example.com").await?;

Why: No network access in the playground.

Solution: For network code, install Rust locally.

Problem:

use some_obscure_crate::Thing; // Not available

Why: Playground has only the top 100 crates.

Available crates:

  • Standard library (always)
  • Popular crates (serde, tokio, regex, etc.)

To check if a crate is available: Look for the “ADD CRATE” button (shows available crates).

Problem:

fn main() {
loop {
// Infinite loop
}
}

Result: Timeout after 30 seconds.

Solution: Write code that completes quickly.


Good use:

// Test how Option works
fn main() {
let x: Option<i32> = Some(5);
match x {
Some(val) => println!("Value: {}", val),
None => println!("No value"),
}
}

Quick test, immediate feedback!

Always click “Format” before sharing code:

  • Makes code readable
  • Follows Rust conventions
  • Professional appearance

Click “Clippy” to get suggestions. Clippy goes beyond compiler errors and flags non-idiomatic code:

fn main() {
let s = String::from("hello");
if s.len() == 0 {
println!("empty");
}
}

Clippy reports:

warning: length comparison to zero
--> src/main.rs:3:8
|
3 | if s.len() == 0 {
| ^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `s.is_empty()`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#len_zero
= note: `#[warn(clippy::len_zero)]` on by default

Learn from suggestions!

When asking for help:

  1. Write minimal example in playground
  2. Click “Share”
  3. Post the link

Example: “I’m having trouble with this: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=abc123

Much better than pasting code in a forum post!


Scenario: You want to test how Rust’s Result type works.

1. Go to playground 2. Write code:

fn divide(a: i32, b: i32) -> Result<i32, String> {
if b == 0 {
Err("Cannot divide by zero".to_string())
} else {
Ok(a / b)
}
}
fn main() {
match divide(10, 2) {
Ok(result) => println!("Result: {}", result),
Err(e) => println!("Error: {}", e),
}
match divide(10, 0) {
Ok(result) => println!("Result: {}", result),
Err(e) => println!("Error: {}", e),
}
}

3. Click “Run” Output:

Result: 5
Error: Cannot divide by zero

4. Click “Format” to clean up 5. Click “Clippy” for suggestions 6. Click “Share” if you want to save it


Open the tools/options menu and choose to show the assembly output.

See the actual machine code generated (x86-64, the Playground’s target):

pub fn add(a: i32, b: i32) -> i32 {
a + b
}

Assembly output:

playground::add:
lea eax, [rdi + rsi]
ret

Why useful: Understanding optimization.

Open the tools/options menu and choose to expand macros (this uses the nightly toolchain).

See what macros expand to:

fn main() {
println!("Hello, {}", "world");
}

Expanded:

fn main() {
{
::std::io::_print(format_args!("Hello, {0}\n", "world"));
};
}

Why useful: Understanding how macros work.

fn add(a: i32, b: i32) -> i32 {
a + b
}
#[test]
fn test_add() {
assert_eq!(add(2, 3), 5);
}
#[test]
fn test_add_negative() {
assert_eq!(add(-1, 1), 0);
}

Click “Test” instead of “Run”:

running 2 tests
test test_add ... ok
test test_add_negative ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

The most reliable shortcut is Ctrl+Enter (run the code). The editor also supports many of the usual code-editor bindings for commenting, moving lines, multi-cursor editing, and find/replace.

ShortcutAction (typical)
Ctrl+EnterRun code
Ctrl+K Ctrl+CComment selection
Ctrl+K Ctrl+UUncomment selection
Ctrl+/Toggle comment
Alt+↑/↓Move line up/down
Ctrl+DSelect next occurrence
Ctrl+FFind
Ctrl+HFind and replace

Note: Editor bindings can differ by browser, platform, and Playground version, so treat the table above as a guide rather than a guarantee. Formatting and Clippy are run from their toolbar buttons. For the current authoritative list, click “Help” in the toolbar.


FeaturePlaygroundLocal (cargo)
InstallationNone neededrustup install
Startup timeInstantcargo run ~0.5s
File system access No Yes
Network access No Yes
Available cratesTop 100 onlyAll crates
Execution time limit30 secondsNo limit
Code sharing Easy (URL)Manual (gist, github)
Editor featuresBasicFull (with rust-analyzer)
Debugging Limited Full (lldb/gdb)
PerformanceSlightly slowerFull native speed

When to use Playground:

  • Learning basics
  • Quick tests
  • Sharing examples
  • No installation needed

When to use local:

  • Real projects
  • Need file system
  • Need specific crates
  • Performance testing


  1. Go to https://play.rust-lang.org/
  2. Modify the default program
  3. Click “Run”
  4. Click “Format”
  5. Click “Share” and copy the URL

Try this code:

fn main() {
let x = 5;
x = 10; // Error!
println!("{}", x);
}

Read the error message. What does it say?

Answer
error[E0384]: cannot assign twice to immutable variable `x`

Variables are immutable by default in Rust!

Fix:

fn main() {
let mut x = 5; // Add 'mut'
x = 10;
println!("{}", x);
}

Write this code:

fn main() {
let s = String::from("hello");
let len = s.len();
println!("{}", len);
}

Click “Clippy”. Does it suggest anything?

Paste this:

fn main(){let x=5;let y=10;println!("{} {}",x,y);}

Click “Format”. See the difference!

  1. Write any Rust code
  2. Click “Share”
  3. Open the link in a private/incognito window
  4. Verify your code is there!

What you’ve learned:

  • How to use the Rust Playground
  • When to use it vs local development
  • How to share code via URL
  • Available tools (Format, Clippy, etc.)
  • Limitations (no file system, network)

Rust Playground features:

  • Instant code execution
  • Compiler error messages
  • Format and lint tools
  • Easy sharing
  • No installation needed

Best for:

  • Learning Rust
  • Quick experiments
  • Sharing examples
  • Teaching

Not suitable for:

  • Real projects
  • File I/O
  • Network requests
  • Large dependencies

URL to remember: https://play.rust-lang.org/

You’re now ready to start learning Rust syntax!