Movatterモバイル変換


[0]ホーム

URL:


Docs.rs

Cratestdweb

Source
Expand description

The goal of this crate is to provide Rust bindings to the Web APIs and to allowa high degree of interoperability between Rust and JavaScript.

§Examples

You can directly embed JavaScript code into Rust:

letmessage ="Hello, 世界!";letresult =js! {    alert( @{message} );return2+2*2;};println!("2 + 2 * 2 = {:?}", result );

Closures are also supported:

letprint_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 toserde:

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

letbutton = document().query_selector("#hide-button").unwrap().unwrap();button.add_event_listener(move|_: ClickEvent| {foranchorindocument().query_selector_all("#main a") {js!( @{anchor}.style ="display: none;"; );    }});

Exposing Rust functions to JavaScript is supported too:

#[js_export]fnhash( string: String ) -> String {letmuthasher = Sha1::new();    hasher.update( string.as_bytes() );    hasher.digest().to_string()}

Then you can do this from Node.js:

var hasher = require( "hasher.js" ); // Where `hasher.js` is generated from Rust code.console.log( hasher.hash( "Hello world!" ) );

Or you can take the same.js file and use it in a web browser:

<script src="hasher.js"></script><script>    Rust.hasher.then( function( hasher ) {        console.log( hasher.hash( "Hello world!" ) );    });</script>

If you’re usingParcel you can also use ourexperimental Parcel plugin;first do this in your existing Parcel project:

$ npm install --save parcel-plugin-cargo-web

And then simply:

import hasher from "./hasher/Cargo.toml";console.log( hasher.hash( "Hello world!" ) );

Modules§

serde
A module with serde-related APIs.
traits
A module containing reexports of all of our interface traits.
unstable
A module containing stable counterparts to currentlyunstable Rust features.
web
A module with bindings to the Web APIs.

Macros§

console
Calls methods on the JavaScriptconsole object.
js
Embeds JavaScript code into your Rust program.
js_deserializable
A macro which makes it possible to convert an instance of a given typeimplementing Serde’sDeserialize into aValue usingTryInto.
js_serializable
A macro which makes it possible to pass an instance of a given typeimplementing Serde’sSerialize into thejs! macro.

Structs§

Array
A type representing a JavaScript array.
DiscardOnDrop
If you have a value which implementsDiscard, you can useDiscardOnDrop::new(value) which will wrap the value.When the wrapper is dropped it will automatically callvalue.discard().
Mut
A wrapper for passingFnMut callbacks into thejs! macro.
Null
A unit type representing JavaScript’snull.
Number
A type representing a JavaScript number.
Object
A type representing a JavaScript object.
Once
A wrapper for passingFnOnce callbacks into thejs! macro.
Reference
A type representing a reference to a JavaScript value.
Symbol
A type representing a JavaScriptSymbol.
Undefined
A unit type representing JavaScript’sundefined.
UnsafeTypedArray
A wrapper type for exposing raw Rust slices asTypedArraysat zero cost without copying.

Enums§

Value
A type representing a JavaScript value.

Traits§

InstanceOf
A trait to check whenever a givenReference is of a certain type.
JsSerialize
A trait for types which can be serialized through thejs! macro.
ReferenceType
A trait for types which wrap a reference to a JavaScript object.

Functions§

event_loop
Runs Emscripten’s event loop.
initialize
Initializes the library.

Attribute Macros§

async_test

[8]ページ先頭

©2009-2025 Movatter.jp