Web Assembly and Rust: cargo build vs wasm-pack

Viewed 1177

Some examples of building wasm use cargo build (like the examples in the book Programming WebAssembly)

cargo build --release --target=wasm32-unknown-unknown

And others use,

wasm-pack build --target web ....

What's the different between these two methods of building a project?

1 Answers

Wasm-pack is a bigger convenience application that provides more than simply building the Rust code.

Amongst other things wasm-pack provides:

  • Building Rust projects to WebAssembly (equivalent to cargo build --target=wasm32-unknown-unknown ...)
  • Binding to Node.js
  • Publishing results to the npm registry.
  • Creating new projects (like cargo new)

This tool seeks to be a one-stop shop for building and working with rust- generated WebAssembly that you would like to interop with JavaScript, in the browser or with Node.js. wasm-pack helps you build rust-generated WebAssembly packages that you could publish to the npm registry, or otherwise use alongside any javascript packages in workflows that you already use, such as webpack or greenkeeper. 1

Related