Skip to main content
Version: 0.18.0

Choosing a web library

Introduction#

Yew apps can be built using either web-sys or stdweb. These two crates provide the bindings between Rust and Web APIs. You'll need to choose one or the other when adding yew to your cargo dependencies:

# Choose `web-sys`yew = "0.17"
# Choose `stdweb`yew = { version = "0.17", package = "yew-stdweb" }

We recommend using web-sys due to its support from the Rust / Wasm Working Group.

warning

Yew will freeze support for stdweb at v0.18. It will still receive patch fixes, but no new features will be added. See #1569

Example Usage#

This example illustrates the difference in how the two libraries are used. You don't need to run this yourself.

// web-syslet window: web_sys::Window = web_sys::window().expect("window not available");window.alert_with_message("hello from wasm!").expect("alert failed");
// stdweblet window: stdweb::web::Window = stdweb::web::window();window.alert("hello from wasm!");
// stdweb with js! macrouse stdweb::js;use stdweb::unstable::TryFrom;use stdweb::web::Window;
let window_val: stdweb::Value = js!{ return window; }; // <- JS syntax inside!let window = Window::try_from(window_val).expect("conversion to window failed");window.alert("hello from wasm!");

The APIs for the two crates differ slightly but they serve roughly the same purpose.

Choosing One#

There are a few different angles to consider when choosing between using web-sys and stdweb for your app. Note that it's possible to use both in one app, but to minimize the binary size of your compiled crate it's best to use only one of the two.

web-sysstdweb
Project StatusActively maintained by the Rust / Wasm Working GroupNo Github activity for over 8 months
Web API CoverageRust APIs are generated from the Web IDL specBrowser APIs are added as needed by the community
Rust API DesignTakes conservative approach by returning Result for most API callsOften avoids Result in favor of panics. For instance, stdweb::web::window() will panic when called in a worker
Supported Build Tools

  • trunk
  • wasm-pack

  • cargo-web
Supported Targets
  • wasm32-unknown-unknown
  • wasm32-unknown-unknown
  • wasm32-unknown-emscripten
  • asmjs-unknown-emscripten