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.

caution

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-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.

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 Status

Actively maintained by the

Rust / Wasm Working Group

No Github activity for over 8 months

Web API Coverage

Rust APIs are generated from the Web IDL spec

Browser APIs are added as needed by the community

Rust API Design

Takes conservative approach by returning Result for most API calls

Often 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