Skip to content

Latest commit

 

History

History

README.md

ICP Rust Recipe Example

This example demonstrates how to use the built-in rust recipe type to build Rust canisters.

Overview

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.

Configuration

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.0

Key Components

  • type: "@dfinity/rust@<version>": Uses the built-in Rust recipe type. See available versions.
  • package: Specifies the Cargo package name to build (required)

Project Structure

  • Cargo.toml: Cargo project configuration with WASM target setup
  • src/lib.rs: Rust canister implementation using ic-cdk
  • .gitignore: Git ignore rules for Rust projects

How It Works

  1. ICP-CLI uses the built-in rust recipe resolver
  2. 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
  3. The canister is built and ready for deployment

Prerequisites

  • Rust toolchain with WASM target support
  • Install via: https://rustup.rs/
  • Add WASM target: rustup target add wasm32-unknown-unknown

Build Process

The recipe automatically:

  1. Compiles the Rust code to WASM using Cargo
  2. Extracts the WASM binary from the target directory
  3. Renames it to match ICP-CLI's expected output format
  4. Injects build metadata using ic-wasm

Use Cases

  • Standard Rust canister development
  • Projects using ic-cdk for Internet Computer development
  • Canisters that don't require custom build logic
  • Multi-package Rust workspaces

Run It

Using the Local Network

# 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