Sharing Classes from Rust with JavaScript
Learn how to import Rust class/struct in JavaScript.
We'll cover the following...
We'll cover the following...
wasm-bindgen enables sharing classes from JavaScript with Rust and vice versa using simple annotations. It handles all the boilerplate stuff, such as translating a value from JavaScript to WebAssembly or WebAssembly to JavaScript, as well as complex memory manipulations and error-prone pointer arithmetic. Thus, wasm-bindgen makes everything easier.
Getting started with the project
Let’s see how easy it is to share classes between JavaScript and WebAssembly (from Rust):
Create a new project:
Define the
wasm-bindgendependency for the project. Open thecargo.tomlfile and add the following content:
Open the
src/lib.rsfile and replace the content with the following:
...