A practical guide to writing Rust-based WebAssembly with wasm-bindgen, sharing hard-won patterns to avoid common pitfalls. Key recommendations include: always pass exported types by reference across the Wasm boundary, use Rc<RefCell<T>> or Arc<Mutex<T>> instead of &mut to avoid re-entrancy issues, never derive Copy on exported wrapper types, prefix Rust-exported types with Wasm* and JS-imported interfaces with Js*, use the wasm_refgen macro to work around collection reference restrictions, and implement From<YourError> for JsValue using js_sys::Error for clean error propagation. The post also covers using duck-typed JS imports to bypass bindgen's reference restrictions and printing build info on startup to ease development.
Table of contents
TL;DRA Quick RefresherNames MatterDon’t Derive CopyAvoiding Broken HandlesDucking Around Reference RestrictionsAutomatically Convert To JS ErrorsPrint Build InfoWrap UpSort: