Note: This page covers Parcel 1, the documentation for Parcel 2 is being worked on here: v2.parceljs.org

Rust

Supported extensions: rs

Rust is a systems programming language developed by Mozilla, which offers native performance with some interesting memory and thread safety characteristics. Rust recently added support for compiling to WebAssembly, and now Parcel makes it super easy to get started with zero configuration!

You can now simply import .rs files just like any other file type in Parcel! Assuming you have Rustup installed, Parcel automatically takes care of installing the right toolchains, targets, and other build pre-requisites. It works with Cargo projects, as well as straight-up Rust source files, automatically tracks your dependencies so files are watched and rebuilds happen when you save, and more!

Just like with .wasm files, .rs files can be imported with either synchronous or asynchonous imports.

// synchronous import
import { add } from './add.rs'
console.log(add(2, 3))
// asynchronous import
const { add } = await import('./add.rs')
console.log(add(2, 3))

On the Rust side, you just need to make sure that function names aren’t mangled and are public.

#[no_mangle]
pub fn add(a: i32, b: i32) -> i32 {
  return a + b
}

You can also import rust project by importing src/lib.rs or src/main.rs, Parcel will call cargo to build the project.

import { sub } from './sub/src/lib.rs'
console.log(sub(2, 3))

in ./sub/Cargo.toml:

[package]
...

[dependencies]

[lib]
crate-type = ["cdylib"]

in ./sub/src/lib.rs:

#[no_mangle]
pub fn sub(a: i32, b: i32) -> i32 {
    a - b
}

See also this complete example.

Help us improve the docs

If something is missing or not entirely clear, please file an issue on the website repository or edit this page.