# Rust error on pre\_upgrade and post\_upgrade attributes

**URL:** <https://forum.dfinity.org/t/rust-error-on-pre-upgrade-and-post-upgrade-attributes/5732>\
**Category:** Developers\
**Created:** [July 6, 2021, 6:26am UTC](https://forum.dfinity.org/t/rust-error-on-pre-upgrade-and-post-upgrade-attributes/5732 "2021-07-06T06:26:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rem.codes](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rem.codes/32/40260_2.png) [@rem.codes](https://forum.dfinity.org/u/rem.codes)\
**Post date:** [July 6, 2021, 6:26am UTC](https://forum.dfinity.org/t/rust-error-on-pre-upgrade-and-post-upgrade-attributes/5732/1 "2021-07-06T06:26:49Z")

</div>

Hi,

as seen in the screenshot copy and pasting (and a little editing) of [cdk-rs/lib.rs at main · dfinity/cdk-rs · GitHub](https://github.com/dfinity/cdk-rs/blob/main/examples/asset_storage/src/asset_storage_rs/lib.rs)

I get a `Post-upgrade function already declared` error, everything works like it should but I really want to resolve this.

I tried renaming the `fn pre_upgrade` to `fn pre_upgrade_something` but it keeps the error.

What is causing the issue? (i’m new to Rust so maybe it’s just a beginner problem)

 ![](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/7/769590e5293676ce73ab458e6d3b979adc5ad0ca.png)

Thanks is advance

---

<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:57am UTC](https://forum.dfinity.org/t/rust-error-on-pre-upgrade-and-post-upgrade-attributes/5732/2 "2021-07-06T07:57:39Z")

</div>

Hi @rem.codes!

> [@rem.codes](#):
>
> everything works like it should

Could you please clarify what you mean by that? Does the canister build when you run `cargo`?

My guess that it might be purely an IDE problem. I wonder if replacing `#[pre_upgrade]` with `#[export_name = "canister_pre_upgrade"]` and `#[post_upgrade]` with `#[export_name = "canister_post_upgrade"]` could help your IDE figure out what’s going on.

---

<div class="post-metadata">

**Author:** ![rem.codes](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rem.codes/32/40260_2.png) [@rem.codes](https://forum.dfinity.org/u/rem.codes)\
**Post date:** [July 6, 2021, 8:46am UTC](https://forum.dfinity.org/t/rust-error-on-pre-upgrade-and-post-upgrade-attributes/5732/3 "2021-07-06T08:46:53Z")

</div>

👋@rom

What i mean by that is, that i can deploy the canister successfully and it works like it should.

When running a `cargo build --verbose` command, it does not build, i thought it was maybe due to running the ic crates locally or something.

i get the following error;

```auto
error: aborting due to previous error; 7 warnings emitted

error: could not compile `user_controller`

Caused by:
  process didn't exit successfully: `rustc --crate-name user_controller --edition=2018 src/backend/rust/user_controller/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type cdylib --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=309fc0f94d246419 --out-dir /home/rmcs/Documents/app/target/debug/deps -C incremental=/home/rmcs/Documents/app/target/debug/incremental -L dependency=/home/rmcs/Documents/app/target/debug/deps --extern
ic_cdk=/home/rmcs/Documents/app/target/debug/deps/libic_cdk-fbc39ac7a95afa8e.rlib --extern
ic_cdk_macros=/home/rmcs/Documents/app/target/debug/deps/libic_cdk_macros-
fb910f7992f28e19.so --extern ic_types=/home/rmcs/Documents/app/target/debug/deps/libic_types-
319e16659f2698fe.rlib --extern serde=/home/rmcs/Documents/app/target/debug/deps/libserde-
65de3c84c101c7a1.rlib` (exit status: 1)

```

The `#[export_name = "canister_post_upgrade"] ` fixed the issue! Thanks! Is there some documentation / github reference on this issue?

---

<div class="post-metadata">

**Author:** ![rem.codes](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rem.codes/32/40260_2.png) [@rem.codes](https://forum.dfinity.org/u/rem.codes)\
**Post date:** [July 6, 2021, 9:31am UTC](https://forum.dfinity.org/t/rust-error-on-pre-upgrade-and-post-upgrade-attributes/5732/4 "2021-07-06T09:31:39Z")

</div>

@roman-kashitsyn in addition to the earlier post  
When i try to build the `rust_hello` from the dfinity tutorials project it also failed.

---

<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, 10:28am UTC](https://forum.dfinity.org/t/rust-error-on-pre-upgrade-and-post-upgrade-attributes/5732/5 "2021-07-06T10:28:48Z")

</div>

> [@rem.codes](#):
>
> When running a `cargo build --verbose` command, it does not build, i thought it was maybe due to running the ic crates locally or something.

I believe this command tries to build a native binary from your canister code, which is not possible at the moment. Specifying the build target explicitly might resolve the issue:

```auto
cargo build --release --target=wasm32-unknown-unknown

```

If you use `rustup`, I also find it useful to add [a toolchain file](https://rust-lang.github.io/rustup/overrides.html#the-toolchain-file) to the project (`rust-toolchain.toml` file living right next to `Cargo.toml`), this is how mine looks like:

```toml
[toolchain]
channel = "1.51.0"
targets = ["wasm32-unknown-unknown"]

```

This will tell cargo to use the right `rustc` version and target by default.

> Is there some documentation / github reference on this issue?

I’m not sure it’s an issue of the Rust CDK, because as you mentioned, the build works fine. For some reason your IDE becomes confused about the code. Unfortunately, I don’t know much about IDEs, maybe some VSCode users could help you understanding the issue better.

---

<div class="post-metadata">

**Author:** ![rem.codes](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rem.codes/32/40260_2.png) [@rem.codes](https://forum.dfinity.org/u/rem.codes)\
**Post date:** [July 6, 2021, 11:04am UTC](https://forum.dfinity.org/t/rust-error-on-pre-upgrade-and-post-upgrade-attributes/5732/6 "2021-07-06T11:04:45Z")

</div>

The `cargo build --release --target=wasm32-unknown-unknown` works to build! Only need to figure out how to use the toolchain file.

Regarding the reference, the IC internet spec states the different options [The Internet Computer Interface Specification :: Internet Computer](https://sdk.dfinity.org/docs/interface-spec/index.html)

Thanks for your help! 🙏

---

<div class="post-metadata">

**Author:** ![RikusWiehahn](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rikuswiehahn/32/4451_2.png) [@RikusWiehahn](https://forum.dfinity.org/u/RikusWiehahn)\
**Post date:** [January 24, 2022, 6:56am UTC](https://forum.dfinity.org/t/rust-error-on-pre-upgrade-and-post-upgrade-attributes/5732/7 "2022-01-24T06:56:51Z")

</div>

Just ran into this myself, (also fairly new to Rust).

Turns out the **Rust for Visual Studio Code** extension is no longer actively maintained, despite having the highest number of installs.

The one called **rust-analyzer** seems to be the unofficial successor, it’s much more up-to-date and doesn’t throw false errors like this one.

Just putting this here so other rust newbies don’t get tripped by this 🙂
