expression

Manage an exported Rust object from JavaScript

An exported Rust struct is represented by generated JavaScript wrapper code. Its Rust allocation has a separate lifetime from the JavaScript wrapper. Use the generated free method when explicitly releasing an owned object, and do not call its methods afterward. This counter uses saturating arithmetic. Expected result (NOT RUN): A new Tally has value 0; after increment it has value 1; host calls free once. Common mistake: Treating the wrapper as a plain serializable object or reusing it after explicit release. Static Rust source module for the accompanying Cargo lab; not a standalone executable or ByteMind exercise.

Formal Syntax & Structure

rust syntaxrust
#[wasm_bindgen] struct; #[wasm_bindgen(constructor)]

Working Example

Example Snippetrust
use wasm_bindgen::prelude::*;#[wasm_bindgen]pub struct Tally { value: u32 }#[wasm_bindgen]impl Tally {    #[wasm_bindgen(constructor)]    pub fn new() -> Tally { Tally { value: 0 } }    pub fn increment(&mut self) { self.value = self.value.saturating_add(1); }    pub fn value(&self) -> u32 { self.value }}
Rust Reference