This example demonstrates how to use the built-in rust recipe type to build Rust canisters.
The rust recipe type provides a streamlined way to build Rust canisters using Cargo with the WASM target. This is one of the built-in recipe types provided by ICP-CLI for common canister development workflows.
The icp.yaml file configures a canister using the built-in rust recipe:
canisters:
- name: backend
recipe:
type: "@dfinity/rust@<version>"
configuration:
# cargo package for canister (required field)
package: backend
shrink: true
# optional
candid: {{ project-name }}.did
metadata:
- name: "crate:version"
value: 1.0.0type: "@dfinity/rust@<version>": Uses the built-in Rust recipe type. See available versions.package: Specifies the Cargo package name to build (required)
Cargo.toml: Cargo project configuration with WASM target setupsrc/lib.rs: Rust canister implementation using ic-cdk.gitignore: Git ignore rules for Rust projects
- ICP-CLI uses the built-in
rustrecipe resolver - The resolver generates build steps that:
- Build the specified Cargo package with
--target wasm32-unknown-unknown --release - Move the resulting WASM file to the output path
- Inject metadata about the Cargo version used
- Build the specified Cargo package with
- The canister is built and ready for deployment
- Rust toolchain with WASM target support
- Install via: https://rustup.rs/
- Add WASM target:
rustup target add wasm32-unknown-unknown
The recipe automatically:
- Compiles the Rust code to WASM using Cargo
- Extracts the WASM binary from the target directory
- Renames it to match ICP-CLI's expected output format
- Injects build metadata using ic-wasm
- Standard Rust canister development
- Projects using ic-cdk for Internet Computer development
- Canisters that don't require custom build logic
- Multi-package Rust workspaces
# Start a local network
icp network start -d
# Build and deploy the canister
icp deploy
# Call the canister
icp canister call backend greet '("Internet Computer")'
# Stop the network
icp network stop