Build Rust canisters using Cargo with WASM target for the Internet Computer.
Example of how to reference this recipe in an icp.yaml file:
canister:
name: backend
recipe:
type: "@dfinity/rust"
configuration:
package: my-canister
shrink: true| Parameter | Type | Required | Description | Default |
|---|---|---|---|---|
| package | string | Yes | Name of the Rust package to build | - |
| metadata | array | No | Array of key-value pairs for custom metadata | [] |
| shrink | boolean | No | Enable WASM optimization to reduce file size | false |
| compress | boolean | No | Enable gzip compression of WASM file | false |
- Rust toolchain with
wasm32-unknown-unknowntarget installed cargobuild systemic-wasmtool must be installed for metadata injection and optimization
# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Add WASM target
rustup target add wasm32-unknown-unknown
# Install ic-wasm
cargo install ic-wasmcanister:
name: hello-rust
recipe:
type: "@dfinity/rust"
configuration:
package: hello-rustcanister:
name: dapp-backend
recipe:
type: "@dfinity/rust"
configuration:
package: dapp-backend
shrink: true
compress: true
metadata:
- name: "crate:version"
value: "1.0.0"
- name: "build:profile"
value: "release"When this recipe is executed:
- Runs
cargo buildwith the specified package for thewasm32-unknown-unknowntarget in release mode - Moves the resulting WASM file from the Cargo target directory to the output location
- Handles package name conversion (hyphens to underscores for the WASM filename)
- Injects Cargo version metadata ("cargo:version")
- Injects template type metadata ("template:type" = "rust")
- Injects any custom metadata specified in the configuration
- Optionally optimizes the WASM file if
shrinkis enabled - Optionally compresses the WASM file if
compressis enabled
A typical Rust canister project structure:
my-project/
├── Cargo.toml # Package configuration
├── src/
│ ├── lib.rs # Main canister code
│ └── types.rs # Type definitions
├── Cargo.lock # Dependency lock file
└── icp.yaml # Build configuration
[package]
name = "my-canister"
version = "1.0.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
ic-cdk = "0.10"
ic-cdk-macros = "0.10"
candid = "0.10"
serde = { version = "1.0", features = ["derive"] }Problem: wasm32-unknown-unknown target not installed
Solution: Run rustup target add wasm32-unknown-unknown to install the WASM target
Problem: Package not found during build
Solution: Verify the package name matches exactly with the name in your Cargo.toml file
Problem: Compilation errors
Solution: Run cargo check locally to identify and fix Rust compilation issues
Problem: WASM file not found after build
Solution: Check that your Cargo.toml has crate-type = ["cdylib"] in the [lib] section
- Motoko Recipe - For building Motoko canisters
- Pre-built Recipe - For using pre-compiled WASM files
- Assets Recipe - For frontend assets canister
Use this recipe when developing IC canisters in Rust, which provides performance benefits and access to the rich Rust ecosystem.
- v1.0.0 - Initial release with Rust compilation support