A standard library for the client-side Web
The goal of this crate is to provide Rust bindings to the Web APIs and to allow a high degree of interoperability between Rust and JavaScript.
Examples
You can directly embed JavaScript code into Rust:
let message = "Hello, 世界!";
let result = js! ;
println!;
Even closures are supported:
let print_hello = ;
js!
You can also pass arbitrary structures thanks to serde:
js_serializable!;
js! ;
This crate also exposes a number of Web APIs, for example:
let button = document.query_selector.unwrap;
button.add_event_listener;
Design goals
- Expose a full suite of Web APIs as exposed by web browsers.
- Try to follow the original JavaScript conventions and structure as much as possible, except in cases where doing otherwise results in a clearly superior design.
- Be a building block from which higher level frameworks and libraries can be built.
- Make it convenient and easy to embed JavaScript code directly into Rust and to marshal data between the two.
- Integrate with the wider Rust ecosystem, e.g. support marshaling of structs which implement serde's Serializable.
- Put Rust in the driver's seat where a non-trivial Web application can be written without touching JavaScript at all.
- Allow Rust to take part in the upcoming WebAssembly (re)volution.
Getting started
WARNING: This crate is still a work-in-progress. Things might not work. Things might break. The APIs are in flux. Please do not use it in production.
-
Add an asmjs target with rustup:
$ rustup target add asmjs-unknown-emscripten
-
Install cargo-web; it's not strictly necessary but it makes things more convenient:
$ cargo install cargo-web
-
Go into
examples/todomvc
and type:$ cargo web start
-
Visit
http://localhost:8000
with your browser.
License
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Snippets of documentation which come from Mozilla Developer Network are covered under the CC-BY-SA, version 2.5 or later.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.