Attribute Macro ic_cdk_macros::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() {
    // ...
}

You can specify a guard function to be executed before the init function. When the guard function returns an error, the init function will not proceed.

fn guard_function() -> Result<(), String> {
    // ...
}
#[init(guard = "guard_function")]
fn init_function() {
    // ...
}

The init function may accept an argument, if that argument is a CandidType:


#[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.