Wasm compilation targets
Quick answer
WebAssembly (Wasm) is a portable instruction format executed by a compatible runtime. Rust can produce Wasm modules for different hosts; selecting a target does not automatically supply host services.
Compile for the intended host
The wasm32-unknown-unknown target makes few assumptions about the host; it has std support but some operating-system operations are unavailable. wasm32-wasip1 targets the WASI Preview 1 interface. A cdylib can expose functions to a host. Rust 2024 requires unsafe(no_mangle) for an unmangled symbol: ensure the export name is unique. The following raw export uses only u32 values, not Rust-specific object layouts.
// SAFETY: this standalone module has no other export named bm_add.#[unsafe(no_mangle)]pub extern "C" fn bm_add(a: u32, b: u32) -> u32 { a.wrapping_add(b) }Manual local Rust/Cargo candidate; not executed. Expected behavior:
Host calling bm_add(3, 4) should receive 7; requires a Wasm build and host instantiation.Wrapping arithmetic gives an explicit overflow policy.
rustup target add wasm32-unknown-unknown --toolchain 1.85.0cargo +1.85.0 build --lib --release --target wasm32-unknown-unknownManual terminal step; not executed. Expected behavior (environment dependent):
Target-specific Wasm artifact if compatible crate and toolchain are configured; network may be needed.Inspect the resulting exports using a compatible Wasm inspection tool; building is not execution.
Common mistake
Do not treat every Wasm target as a browser target.
Try it yourself
Choose a target for a browser calculator and one for a WASI command-line tool; justify each.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Wasm compilation targets — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A Wasm module should act as a WASI Preview 1 CLI. Which target matches the lesson?
Wasm compilation targets — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust // SAFETY: this standalone module has no other export named bm_add. #[unsafe(no_mangle)] pub extern "C" fn bm_add(a: u32, b: u32) -> u32 { a.wrapping_add(b) } ``` Which outcome or interpretation best matches this example?
Wasm compilation targets — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```bash rustup target add wasm32-unknown-unknown --toolchain 1.85.0 cargo +1.85.0 build --lib --release --target wasm32-unknown-unknown ``` Which outcome or interpretation best matches this example?