ic_cdk

Attribute Macro init

Source
#[init]
Expand description

Register the canister_init entry point of a canister.

This attribute macro will export the function canister_init in the canister module.

The function under this attribute must have no return value.

Each canister can only have one canister_init entry point.

ยงExample

#[init]
fn init_function() {
    // ...
}

The init function may accept an argument.

The argument must implement the CandidType trait.

And it should match the initialization parameters of the service constructor in the Candid interface.

#[derive(Clone, Debug, CandidType, Deserialize)]
struct InitArg {
    foo: u8,
}

#[init]
fn init_function(arg: InitArg) {
    // ...
}

In this case, the argument will be read from ic0.msg_arg_data_size/copy and passed to the init function upon successful deserialization.

Refer to the canister_init Specification for more information.