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.
You can directly embed JavaScript code into Rust:
```rust let message = "Hello, 世界!"; let result = js! { alert!( @{message} ); return 2 + 2 * 2; };
println!( "2 + 2 * 2 = {:?}", result ); ```
Even closures are are supported:
```rust let print_hello = |name: String| { println!( "Hello, {}!", name ); };
js! { var printhello = @{printhello}; printhello( "Bob" ); printhello.drop(); // Necessary to clean up the closure on Rust's side. } ```
You can also pass arbitrary structures thanks to [serde]:
```rust
struct Person { name: String, age: i32 }
js_serializable!( Person );
js! { var person = @{person}; console.log( person.name + " is " + person.age + " years old." ); }; ```
This crate also exposes a number of Web APIs, for example:
rust
let button = document().query_selector( "#hide-button" ).unwrap();
button.add_event_listener( move |_: ClickEvent| {
for anchor in document().query_selector_all( "#main a" ) {
js!( @{anchor}.style = "display: none;"; );
}
});
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 emscripten. If you're on Arch Linux then you can just
run sudo pacman -S emscripten
; other distributions might also
have recent enough emscripten packages in their repositories.
Alternatively you can install it like this:
$ curl -O https://s3.amazonaws.com/mozilla-games/emscripten/releases/emsdk-portable.tar.gz $ tar -xzf emsdk-portable.tar.gz $ source emsdkportable/emsdkenv.sh $ emsdk update $ emsdk install sdk-incoming-64bit $ emsdk activate sdk-incoming-64bit
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.
Licensed under either of
at your option.
Snippets of documentation which come from [Mozilla Developer Network] are covered under the [CC-BY-SA, version 2.5] or later.
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.