# Rust canister init function not called

**URL:** <https://forum.dfinity.org/t/rust-canister-init-function-not-called/4236>\
**Category:** Developers\
**Created:** [May 23, 2021, 11:36pm UTC](https://forum.dfinity.org/t/rust-canister-init-function-not-called/4236 "2021-05-23T23:36:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![senior.joinu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/senior.joinu/32/9240_2.png) [@senior.joinu](https://forum.dfinity.org/u/senior.joinu)\
**Post date:** [May 23, 2021, 11:36pm UTC](https://forum.dfinity.org/t/rust-canister-init-function-not-called/4236/1 "2021-05-23T23:36:05Z")

</div>

I have a simple canister with the code like this

```auto
static mut TOKEN: Option<VotingToken> = None;

#[init]
fn init() {
    ic_cdk::print("!!! Calling init");

    unsafe {
        TOKEN = Some(VotingToken {
            name: "Default token".into(),
            balances: HashMap::new(),
        });
    }
}

```

But it seems like `init()` function is not called automatically after canister deployment. What am I doing wrong?

Another question: on the docs site there are several rust canister tutorials, two of which propose different storage management approaches ([Adding and searching simple records :: Internet Computer](https://sdk.dfinity.org/docs/rust-guide/rust-profile.html) / [Incrementing a counter :: Internet Computer](https://sdk.dfinity.org/docs/rust-guide/rust-counter.html)). What’s the difference?

---

<div class="post-metadata">

**Author:** ![senior.joinu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/senior.joinu/32/9240_2.png) [@senior.joinu](https://forum.dfinity.org/u/senior.joinu)\
**Post date:** [May 24, 2021, 12:00am UTC](https://forum.dfinity.org/t/rust-canister-init-function-not-called/4236/2 "2021-05-24T00:00:28Z")

</div>

Nevermind, was using the old version of DFX.

But the second question still matters.

---

<div class="post-metadata">

**Author:** ![senior.joinu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/senior.joinu/32/9240_2.png) [@senior.joinu](https://forum.dfinity.org/u/senior.joinu)\
**Post date:** [May 24, 2021, 6:11pm UTC](https://forum.dfinity.org/t/rust-canister-init-function-not-called/4236/3 "2021-05-24T18:11:55Z")

</div>

Hm… it looks like it doesn’t get called on upgrade also.  
After a canister upgrade there is no `TOKEN` anymore - it is uninitialized.

dfx 0.7.0

---

<div class="post-metadata">

**Author:** ![flyq](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/flyq/32/3637_2.png) [@flyq](https://forum.dfinity.org/u/flyq)\
**Post date:** [June 5, 2021, 7:32am UTC](https://forum.dfinity.org/t/rust-canister-init-function-not-called/4236/4 "2021-06-05T07:32:55Z")

</div>

Yes, there are too few examples about rust canister.

about your first question, I run in 0.7.0 dfx and it inits succeed:

```auto
#[init]
fn init() {
    unsafe {
        COUNTER = Some(candid::Nat::from(0));
    }
}

```

```auto
sudo dfx canister call hello_rs get
(0)

```

the result is (0), not (null).

The second question I am also very confused, when do I need to choose different storage? What are their advantages and disadvantages.

The problem I have is that if I want to enter a parameter during initialization, I don’t know how to do it:

```auto
#[init]
fn init(a : candid::Nat) {
    unsafe {
        COUNTER = Some(candid::Nat::from(a));
    }
}

```

the results:

```auto
sudo dfx canister install hello_rs --argument='(1)' -m=reinstall
Reinstalling code for canister hello_rs, with canister_id ryjl3-tyaaa-aaaaa-aaaba-cai
The invocation to the wallet call forward method failed with the error: An error happened during the call: 5: Canister ryjl3-tyaaa-aaaaa-aaaba-cai trapped explicitly: Custom("No more values to deserialize")

sudo dfx canister install hello_rs -m=reinstall
Reinstalling code for canister hello_rs, with canister_id ryjl3-tyaaa-aaaaa-aaaba-cai
The invocation to the wallet call forward method failed with the error: An error happened during the call: 5: Canister ryjl3-tyaaa-aaaaa-aaaba-cai trapped explicitly: Custom("No more values to deserialize")

```

---

<div class="post-metadata">

**Author:** ![flyq](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/flyq/32/3637_2.png) [@flyq](https://forum.dfinity.org/u/flyq)\
**Post date:** [June 6, 2021, 5:05am UTC](https://forum.dfinity.org/t/rust-canister-init-function-not-called/4236/5 "2021-06-06T05:05:08Z")

</div>

> [@flyq](#):
>
> The problem I have is that if I want to enter a parameter during initialization, I don’t know how to do it

change the did file to:

```did
service : (nat) -> {
  "increment": () -> ();
  "get": () -> (nat) query;
  "set": (nat) -> ();
}

```

and try `dfx build --all`

---

<div class="post-metadata">

**Author:** ![jboulineau](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jboulineau/32/1981_2.png) [@jboulineau](https://forum.dfinity.org/u/jboulineau)\
**Post date:** [July 5, 2021, 2:50pm UTC](https://forum.dfinity.org/t/rust-canister-init-function-not-called/4236/6 "2021-07-05T14:50:38Z")

</div>

I’m looking at the ‘incrementing a counter’ example and it looks like this may not be working again in 0.7.2.

Expected Results:

```auto
dfx canister call rust_counter get
(0)

```

Actual Results:

```auto
dfx canister call rust_counter get
[Canister ryjl3-tyaaa-aaaaa-aaaba-cai] Panicked at 'called `Option::unwrap()` on a `None` value'

```

Using debug prints I was able to confirm that init() is not called on canister startup.

---

<div class="post-metadata">

**Author:** ![jboulineau](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jboulineau/32/1981_2.png) [@jboulineau](https://forum.dfinity.org/u/jboulineau)\
**Post date:** [July 5, 2021, 8:57pm UTC](https://forum.dfinity.org/t/rust-canister-init-function-not-called/4236/7 "2021-07-05T20:57:20Z")

</div>

Interesting behavior: when `dfx.json` has two canisters to deploy, init() IS called.

---

<div class="post-metadata">

**Author:** ![roman-kashitsyn](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/roman-kashitsyn/32/3802_2.png) [@roman-kashitsyn](https://forum.dfinity.org/u/roman-kashitsyn)\
**Post date:** [July 6, 2021, 7:44am UTC](https://forum.dfinity.org/t/rust-canister-init-function-not-called/4236/8 "2021-07-06T07:44:52Z")

</div>

Hi @jboulineau!

I believe I need to clarify the canister lifecycle a bit.

1. The first time you install canister code, the `init` function is called.
2. If you decide to wipe out the old version of your canister and replace it with new code (by specifying `--mode=reinstall` option of the dfx canister install command), the `init` function is called after the new code is installed.
3. If you upgrade your canister, the `init` function is _not_ called. The system first calls `canister_pre_upgrade` function on the old version of the canister and then calls `canister_post_upgrade` on the new version.

This means that if you don’t specify upgrade hooks that use stable memory and upgrade the canister, all the state is lost. In case of something `static mut TOKEN: Option<VotingToken> = None;`, `TOKEN` becomes `None` again.

So my guess is that you upgraded your canister after the first installation without any upgrade hooks. This wiped away the initialized state, leading to this error. Re-installing the canister should fix the issue.

> Interesting behavior: when dfx.json has two canisters to deploy, init() IS called.

Most likely, in this case a new canister was created, which triggered the (1) case in the lifecycle above.

---

<div class="post-metadata">

**Author:** ![jboulineau](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jboulineau/32/1981_2.png) [@jboulineau](https://forum.dfinity.org/u/jboulineau)\
**Post date:** [July 6, 2021, 8:22pm UTC](https://forum.dfinity.org/t/rust-canister-init-function-not-called/4236/9 "2021-07-06T20:22:13Z")

</div>

Thanks @roman-kashitsyn! That was the missing piece for me.
