stdweb
02 / 02

stdweb Fundamentals: Bridging Rust and the Browser

stdweb: Bridging Rust and the Browser

stdweb is a Rust crate providing bindings to browser Web APIs -- DOM manipulation, events, timers -- letting Rust code compiled to WebAssembly interact with a web page. WebAssembly itself has no built-in concept of the DOM, so a binding library is what bridges the two.

DOM Manipulation From Rust

use stdweb::web::{document, INode};

let body = document().body().unwrap();
let div = document().create_element("div").unwrap();
div.set_text_content("Hello from Rust!");
body.append_child(&div);

// Rust code performing the same kinds of DOM operations
// JavaScript natively supports

The js! Macro: A Flexible Escape Hatch

let name = "Ada";
js! {
  alert("Hello, " + @{name} + "!");
}

// Embeds literal JavaScript inline, with Rust values passed
// in via @{...} -- covers browser API surface stdweb's typed
// bindings don't directly wrap

Typed Bindings vs. js!: A Trade-off

A typed binding (where available) offers stronger compile-time checking; embedded raw JavaScript snippets via js! bypass that type checking for that specific interaction -- a real trade-off between flexibility/coverage and full compile-time safety.

Keep your own version of these notes — editable, searchable, and organised by your stack.

Start free