<p align="center">
<img src="info/logo.png">
</p>
[](https://travis-ci.org/koute/stdweb)
[](https://gitter.im/stdweb-rs/stdweb?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
# A standard library for the client-side Web
[](https://docs.rs/stdweb/*/stdweb/)
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:
```rust
let message = "Hello, 世界!";
let result = js! {
alert( @{message} );
return 2 + 2 * 2;
};
println!( "2 + 2 * 2 = {:?}", result );
```
Even closures are supported:
```rust
let print_hello = |name: String| {
println!( "Hello, {}!", name );
};
js! {
var print_hello = @{print_hello};
print_hello( "Bob" );
print_hello.drop(); // Necessary to clean up the closure on Rust's side.
}
```
You can also pass arbitrary structures thanks to [serde]:
```rust
#[derive(Serialize)]
struct Person {
name: String,
age: i32
}
js_serializable!( Person );
js! {
var person = @{person};
console.log( person.name + " is " + person.age + " years old." );
};
```
[serde]: https://serde.rs/
This crate also exposes a number of Web APIs, for example:
```rust
let button = document().query_selector( "#hide-button" ).unwrap();
* `target/wasm32-unknown-unknown/release/hasher.js`
* `target/wasm32-unknown-unknown/release/hasher.wasm`
You can copy them into your JavaScript project and load like any other JavaScript file:
```html
<script src="hasher.js"></script>
```
After it's loaded you can access `Rust.hasher`, which is a [Promise] which
will resolve once the WebAssembly module is loaded. Inside that promise
you'll find the contents of `Module.exports` which we've set from our
Rust code, which includes our exported function which you can now call:
```html
<script>
Rust.hasher.then( function( hasher ) {
const string = "fiddlesticks";
const hash = hasher.sha1( string );
console.log( "Hash of " + string + " is '" + hash + "'" );
});
</script>
```
You can also use the very same `hasher.js` from Nodejs:
```js
const hasher = require( "hasher.js" );
const string = "fiddlesticks";
const hash = hasher.sha1( string );
console.log( "Hash of " + string + " is '" + hash + "'" );
```
For the Nodejs environment the WebAssembly is compiled synchronously.
[Promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
## License
Licensed under either of
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](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.
[Mozilla Developer Network]: https://developer.mozilla.org/en-US/
[CC-BY-SA, version 2.5]: https://developer.mozilla.org/en-US/docs/MDN/About#Copyrights_and_licenses
### 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.