Back to Wasm Bindgen

web-sys: Closures

guide/src/examples/closures.md

0.2.1201.7 KB
Original Source

web-sys: Closures

View full source code or view the compiled example online

The Closure type allows passing Rust closures to JavaScript. This example demonstrates different Closure APIs for various use cases.

Choosing a Closure API

  • &dyn Fn / &mut dyn FnMut — For immediate/synchronous callbacks where JavaScript calls the closure right away and doesn't retain it. Lightweight with no JS wrapper overhead. These are unwind safe by default.

  • ScopedClosure::borrow / ScopedClosure::borrow_mut — For known-lifetime callbacks where JavaScript may briefly retain the closure but you control when it becomes invalid. borrow is for immutable Fn, borrow_mut is for mutable FnMut.

  • Closure::new — For indeterminate-lifetime closures like event handlers or timers. The closure must be 'static and you must manage its lifetime (store it somewhere or call forget()).

  • Closure::once / Closure::once_into_js — For one-shot callbacks that will only be called once.

All closure types are unwind safe — panics are caught and converted to JavaScript exceptions when built with panic=unwind. This requires the closure to implement UnwindSafe. If your closure captures non-unwind-safe types (like Rc<RefCell<_>>), use AssertUnwindSafe to wrap the closure, or use the *_aborting variants which don't require UnwindSafe.

See Passing Rust Closures to JS for detailed documentation.

src/lib.rs

rust
{{#include ../../../examples/closures/src/lib.rs}}