36 releases (13 breaking)

new 0.18.0-alpha.1 Feb 26, 2025
0.17.1 Dec 19, 2024
0.17.0 Nov 5, 2024
0.15.0 Jul 1, 2024
0.1.1 Oct 4, 2020

#6 in #dfinity

Download history 25615/week @ 2024-10-30 23047/week @ 2024-11-06 25822/week @ 2024-11-13 32004/week @ 2024-11-20 31107/week @ 2024-11-27 31559/week @ 2024-12-04 32793/week @ 2024-12-11 18113/week @ 2024-12-18 5680/week @ 2024-12-25 17968/week @ 2025-01-01 28817/week @ 2025-01-08 32283/week @ 2025-01-15 30020/week @ 2025-01-22 29641/week @ 2025-01-29 35304/week @ 2025-02-05 20602/week @ 2025-02-12

120,883 downloads per month
Used in 149 crates (125 directly)

Apache-2.0

330KB
5K SLoC

Documentation Crates.io License Downloads CI

ic-cdk

Canister Developer Kit for the Internet Computer.

Background

On the Internet Computer, smart contracts come in the form of canisters which are WebAssembly modules.

Canisters expose entry points which can be called both by other canisters and by parties external to the IC.

This library aims to provide a Rust-ergonomic abstraction to implement Canister entry points.

Using ic-cdk

In Cargo.toml:

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

[dependencies]
ic-cdk = "0.18"
candid = "0.10" # required if you want to define Candid data types

Then in Rust source code:

#[ic_cdk::query]
fn hello() -> String {
    "world".to_string()
}

This will register a query entry point named hello.

Macros

This library re-exports macros defined in ic-cdk-macros crate.

The macros fall into two categories:

  • To register functions as canister entry points
  • To export Candid definitions

Register functions as canister entry points

These macros are directly related to the Internet Computer Specification.

Canister entry points can be async. The CDK embeds an asynchronous executor. Unfortunately anything tokio-specific cannot be used. Use the spawn function to run more asynchronous functions in the background. Panics can cause async tasks to cancel partway through; read the documentation for the futures module for more information.

Export Candid definitions

Check Generating Candid files for Rust canisters for more details.

More examples

The examples repository offers numerous Rust examples demonstrating how to build functional Rust canisters.

Manage Data Structures in Stable Memory

For managing larger datasets and multiple data structures in stable memory, consider using the ic-stable-structures crate. While the ic_cdk::storage::{stable_save, stable_restore} API is straightforward, it may not be efficient for larger datasets. The ic-stable-structures crate provides more scalable solutions for such scenarios.

Dependencies

~6–14MB
~145K SLoC