Crate stdweb [] [src]

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! {
    alert( @{message} );
    return 2 + 2 * 2;
};

println!( "2 + 2 * 2 = {:?}", result );

Even closures are supported:

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:

#[derive(Serialize)]
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:

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;"; );
    }
});

Modules

serde

A module with serde-related APIs.

unstable

A module containing stable counterparts to currently unstable Rust features.

web

A module with bindings to the Web APIs.

Macros

js

Embeds JavaScript code into your Rust program.

js_deserializable

A macro which makes it possible to convert an instance of a given type implementing Serde's Deserialize into a Value using TryInto.

js_serializable

A macro which makes it possible to pass an instance of a given type implementing Serde's Serialize into the js! macro.

Structs

Null

A unit type representing JavaScript's null.

Number

A type representing a JavaScript number.

Reference

A type representing a reference to a JavaScript value.

Undefined

A unit type representing JavaScript's undefined.

Enums

Value

A type representing a JavaScript value.

Functions

event_loop

Runs the event loop.

initialize

Initializes the library.