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:

  1. # Choose `web-sys`
  2. yew = "0.17"
  3. # Choose `stdweb`
  4. yew = { version = "0.17", package = "yew-stdweb" }

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

Choosing a web library - 图1caution

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.

  1. // web-sys
  2. let window: web_sys::Window = web_sys::window().expect("window not available");
  3. window.alert_with_message("hello from wasm!").expect("alert failed");
  4. // stdweb
  5. let window: stdweb::web::Window = stdweb::web::window();
  6. window.alert("hello from wasm!");
  7. // stdweb with js! macro
  8. use stdweb::js;
  9. use stdweb::unstable::TryFrom;
  10. use stdweb::web::Window;
  11. let window_val: stdweb::Value = js!{ return window; }; // <- JS syntax inside!
  12. let window = Window::try_from(window_val).expect("conversion to window failed");
  13. 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