Converting Rust into WebAssembly via Cargo

Learn how to create a new Rust project and run it using the cargo command.

We’ve seen how to use rustc to generate WebAssembly modules. It uses Emscripten behind the scenes to create them. But Rust provides another abstraction to generate WebAssembly modules via Cargo.

Cargo makes it easier to create, run, download, compile, test, and run your project. The cargo command provides a wrapper that calls the rustc compiler to start the compilation. To create WebAssembly modules using Rust’s toolchain, we’ll use a different target, wasm32-unknown-unknown.

The wasm32-unknown-unknown target adds zero runtime and toolchain footprint. wasm32 makes the compiler assume that only the wasm32 instruction set is present. The first unknown in unknown-unknown indicates that the code can compile on any machine, and the second indicates that the code can run on any machine.

Getting started with the project

To see it in action, let’s create a new project with Cargo:

Get hands-on with 1200+ tech skills courses.