Runnable Code Blocks (API Playground)
Code blocks marked with run execute directly in the browser, with results shown inline. No backend required — JavaScript runs on the page, and Rust code is compiled and run through the public Rust Playground API.
Running requires the browser to reach the target API. The Rust Playground and GitHub API occasionally time out in mainland China; on failure the error panel shows troubleshooting hints — just retry later.
Running Rust
Click the "Run" button below. The code is sent to the Rust Playground, and the panel shows program stdout plus cargo build logs (stderr):
fn main() {
let pins = ["GPIO1_A0", "GPIO1_A1", "I2C1_SDA", "I2C1_SCL"];
for (i, pin) in pins.iter().enumerate() {
println!("pin {i}: {pin}");
}
let sum: u32 = (1..=100).sum();
println!("1+2+...+100 = {sum}");
}
A compile error is not a "request failure" — it still runs and shows the error in stderr:
fn main() {
let x: u32 = "not a number";
println!("{x}");
}
Override compiler options via the metastring (edition / channel / mode):
fn main() {
// A simple example that behaves the same on 2021 and 2024 editions
let v = vec![1, 2, 3];
let total: i32 = v.iter().sum();
println!("total = {total}");
}
Running JavaScript
js run blocks execute on the page. Top-level await works, and both fetch results and console output are captured:
const res = await fetch('https://api.github.com/repos/facebook/docusaurus');
const repo = await res.json();
console.log('stars:', repo.stargazers_count);
console.log('topics:', repo.topics);
Send a POST request:
const res = await fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({title: 'hello', body: 'from browser', userId: 1}),
});
console.log('status:', res.status);
console.log(await res.json());
Author guide
Syntax
| Markup | Effect |
|---|---|
```js run | Execute JS on the page (new Function, top-level await supported) |
```rust run | Submit to the Rust Playground |
```rust run edition=2024 channel=stable mode=release | Override playground options (defaults: stable/debug/2024) |
Limitations
- CORS: the JS path requires the target API to allow cross-origin requests (
Access-Control-Allow-Origin), otherwise the browser blocks them — the error panel explains. - Timeouts: 15s per request, 30s overall for JS, 60s for Rust (including compilation).
- Local cache: successful results are cached in localStorage (7 days, max 100 entries); a cache hit skips the request entirely. Editing the snippet or its options invalidates the key automatically. Use "Run again" on the panel to bypass the cache.
- The Rust Playground is a free third-party service (no SLA, occasional rate limits); it compiles a single-file
bincrate with no network or device access. - CSP:
js runrelies onunsafe-eval; a future strict CSP would break the JS path (rust is unaffected).