Importing the JavaScript Function into Rust

Learn how to use the JavaScript function inside Rust code.

In certain places, JavaScript is faster than WebAssembly because there is no overhead of boundary crossing and instantiating a separate runtime environment. JavaScript runs more naturally in its own environment. The JavaScript ecosystem is huge. There are millions of libraries created and battle tested (not all of them, of course) with JavaScript. This makes JavaScript easy (although “easy” here is subjective).

WebAssembly addresses the most important problem that we have in the front-end world, that of “consistent” performance. But it’s not a complete replacement for JavaScript. WebAssembly helps JavaScript to deliver better and more consistent performance.

JavaScript will be a default choice in most places, but it’s important to provide an ecosystem that allows seamless integration between the two. We’ve already seen how to import a class from JavaScript into Rust. Similarly, we can import anything from JavaScript into Rust using wasm-bindgen. The most important part is that we can more naturally use these imported JavaScript functions inside Rust code.

Getting started with a project

In this example, let’s see how to import a JavaScript function into Rust:

  1. Create a new project:

Get hands-on with 1200+ tech skills courses.