expression
Update a browser element using web-sys
web-sys provides feature-gated bindings to browser APIs. The DOM is a host capability, not a feature available in every Wasm runtime. This function finds an element with a fixed identifier and writes text instead of interpreting HTML. Missing window, document or element is handled explicitly. Expected result (NOT RUN): In the browser lab, status text becomes Ready; markup-shaped text stays text. Common mistake: Assuming browser APIs exist in Node or interpreting untrusted text as HTML. Static Rust source module for the accompanying Cargo lab; not a standalone executable or ByteMind exercise.
Formal Syntax & Structure
rust syntaxrust
web_sys::window(); Document::get_element_by_id; Node::set_text_contentWorking Example
Example Snippetrust
use wasm_bindgen::prelude::*;#[wasm_bindgen]pub fn show_status(message: &str) -> Result<(), JsValue> { let window = web_sys::window().ok_or_else(|| JsValue::from_str("no window"))?; let document = window.document().ok_or_else(|| JsValue::from_str("no document"))?; let element = document.get_element_by_id("status").ok_or_else(|| JsValue::from_str("missing status"))?; element.set_text_content(Some(message)); Ok(())}