Choose web-sys or stdweb

Introduction

Yew apps can be built with 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:

{% code title="Cargo.toml" %}


#![allow(unused_variables)]
fn main() {
Choose `web-sys`
yew = "0.15"

Choose `stdweb`
yew = { version = "0.15", package = "yew-stdweb" }
}

{% endcode %}

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

Example Usage


#![allow(unused_variables)]
fn main() {
// web-sys
let window: web_sys::Window = web_sys::window().expect("window not available");
window.alert_with_message("hello from wasm!").expect("alert failed");

// stdweb
let window: stdweb::web::Window = stdweb::web::window();
window.alert("hello from wasm!");

// stdweb with js! macro
use 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 with similar functionality.

Choosing One

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

web-sysstdweb
Project StatusActively maintained by the Rust / Wasm Working GroupNo Github activity for over 4 months
Web API CoverageRust APIs are auto-generated from the Web IDL spec and so should have 100% coverage.Browser 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
  • wasm-bindgen
  • wasm-pack
  • cargo-web
  • wasm-bindgen
  • wasm-pack
Supported Targets
  • wasm32-unknown-unknown
  • wasm32-unknown-unknown
  • wasm32-unknown-emscripten
  • asmjs-unknown-emscripten