Rust (Wasm)
Rust is a general-purpose programming language designed for performance and safety.
In LiveCodes, Rust runs in the browser by interpreting the source with Miri (the Rust mid-level IR interpreter) compiled to WebAssembly. No backend is involved.
Usage
Demo:
Communication with JavaScript
The Rust code runs in the context of the result page. A few helper properties and methods are available in the browser global livecodes.rust object:
livecodes.rust.input: The standard input passed to the Rust program. It can be set before the initial run, or passed torunfor subsequent runs.livecodes.rust.loaded: A promise that resolves when the Rust environment (WebAssembly interpreter and standard library) is fully loaded (and rejects if it fails to load). Other helpers should be used after this promise resolves.livecodes.rust.output: The standard output from the Rust code execution.livecodes.rust.exitCode: The exit code of the last run.livecodes.rust.run: A function that runs the Rust code again with new input. It takes a string as input and returns a promise that resolves with an object containing theoutput,error, andexitCodeproperties.
Example:
Language Info
Name
rust-wasm
Aliases / Extensions
rust, rs, rust-wasm, rs-wasm, wasm.rs
Editor
script
Compiler
Miri, the Rust mid-level IR interpreter, compiled to WebAssembly.
Version
Miri built against Rust 1.79 (2024-06-15).
Code Formatting
Using Prettier with the Prettier Rust plugin.
Limitations
- Programs are single-file. Crates and external dependencies are not supported.
- Execution is interpreted, so it is considerably slower than native Rust.
- Threads (
std::thread) are not supported. - Undefined-behaviour checks are disabled in this build, because a playground is interested in whether a program works rather than whether it is sound.
Live Reload
By default, new code changes are sent to the result page for re-evaluation without a full page reload, avoiding the need to reinitialize the Rust WebAssembly environment. This behavior can be disabled by adding the code comment // __livecodes_reload__ to the Rust code, which forces a full page reload.
This comment can be added in the hiddenContent property of the editor for embedded playgrounds.
Starter Template
https://livecodes.io/?template=rust-wasm