Generating Candid files for Rust canisters
Beginner
Rust
Candid interface files (.did
) are not generated automatically for Rust canisters like they are for Motoko canisters. For Rust canisters, you must write the .did
file yourself or you can use a tool like the candid-extractor to extract the Candid from the canister's Wasm.
Install the candid-extractor with cargo:
cargo install candid-extractor
Then, in your Rust canister code, call the export_candid
macro at the end of your lib.rs
file:
lib.rs
use ic_cdk::query;
use ic_cdk::update;
#[query]
fn hello(name: String) -> String {
format!("Hello, {}!", name)
}
#[update]
fn world(name: String) -> String {
format!("World, {}!", name)
}
// Enable Candid export
ic_cdk::export_candid!();
Before extracting the Candid interface, you must first compile the canister's Wasm module:
cargo build --release --target wasm32-unknown-unknown --package <CANISTER>
The Wasm module will be stored at:
target/wasm32-unknown-unknown/release/<CANISTER>.wasm
Then you can extract the Candid interface from the Wasm module and save it to a file:
candid-extractor target/wasm32-unknown-unknown/release/<CANISTER>.wasm > <CANISTER>.did