Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
121 lines (103 sloc) 3.84 KB

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" %}

# Choose `web-sys`
yew = { version = "0.13", features = ["web_sys"] }

# Choose `stdweb`
yew = { version = "0.13", features = ["std_web"] }

{% endcode %}

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

Example Usage

// 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-sys stdweb
Project Status Actively maintained by the Rust / Wasm Working Group No Github activity for over 4 months
Web API Coverage Rust 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 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
  • wasm-bindgen
  • wasm-pack
  • cargo-web
  • wasm-bindgen
  • wasm-pack
Supported Targets
  • wasm32-unknown-unknown
  • wasm32-unknown-unknown
  • wasm32-unknown-emscripten
  • asmjs-unknown-emscripten
For more information about picking a build tool, checkout the [Wasm Build Tools](project-setup/#wasm-build-tools) guide.
You can’t perform that action at this time.