Sharing Classes from JavaScript with Rust

Learn how to use JavaScript classes in the Rust project.

Sharing JavaScript classes with Rust is also made easy with #[wasm_bindgen]. Let’s look at how to achieve it.

JavaScript classes are objects with some methods. Rust is a strictly typed language. This means the Rust compiler needs to have concrete bindings. Without them, the compiler complains, because it needs to know about the lifetime of an object. We need a way to ensure the compiler has this API available at runtime.

The extern C function block helps out here. The extern C makes a function name available in Rust.

Getting started with the project

In this example, let’s see how to share a class from JavaScript with Rust:

  1. Let’s create a new project:

Get hands-on with 1400+ tech skills courses.