# Question about defining an update method with rust

**URL:** <https://forum.dfinity.org/t/question-about-defining-an-update-method-with-rust/12350>\
**Category:** Rust\
**Created:** [April 22, 2022, 2:53pm UTC](https://forum.dfinity.org/t/question-about-defining-an-update-method-with-rust/12350 "2022-04-22T14:53:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ildefons](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ildefons/32/2638_2.png) [@ildefons](https://forum.dfinity.org/u/ildefons)\
**Post date:** [April 22, 2022, 2:53pm UTC](https://forum.dfinity.org/t/question-about-defining-an-update-method-with-rust/12350/1 "2022-04-22T14:53:24Z")

</div>

When defining an update method with rust, what is the difference between

1. #[update],
2. #[update(guard = “is\_user”)],  
and
3. #[update(name = “funtionName”)] ?

---

<div class="post-metadata">

**Author:** ![GLdev](https://avatars.discourse-cdn.com/v4/letter/g/4bbf92/32.png) [@GLdev](https://forum.dfinity.org/u/GLdev)\
**Post date:** [April 23, 2022, 8:12am UTC](https://forum.dfinity.org/t/question-about-defining-an-update-method-with-rust/12350/2 "2022-04-23T08:12:52Z")

</div>

`#update`  
This marks the following fn as an update call and uses its name. If your next line is  
`fn my_awesome_update_func` you could call your canister with this name and it would perform the update.

`#[update(guard = “is_user”)]`

The guard parameter can be read as this, in layman’s terms: Before you run my function, go run is\_user, and only run my function if the guard returned OK().

Here’s a guard example function:

```auto
fn is_controller() -> Result<(), String> {
    RUNTIME_STATE.with(|state| {
        if state
            .borrow()
            .data
            .canister_settings
            .controllers
            .contains(&state.borrow().env.caller())
        {
            Ok(())
        } else {
            Err("You are not a controller".to_string())
        }
    })
}

```

And I use it like so:

```auto
#[update(name = "sendCycles", guard = "is_controller")]

async fn send_cycles() -> bool {
[...]

```

So my send\_cycles fn gets “renamed” to sendCycles (as the js convention uses camelCase) and I also guard this function so it can be called only by the canister’s controllers.

I think I answered 2 & 3 with one example.

Also, while we’re talking about this, it’s worth mentioning the following macro:

```auto
#[candid_method(update, rename = "stop_cron")]

```

By annotating functions with this macro, we can then use the self rust → candid interface thingy, like so:

```auto
// Auto export the candid interface

candid::export_service!();

#[query(name = "__get_candid_interface_tmp_hack")]

#[candid_method(query, rename = "__get_candid_interface_tmp_hack")]

fn __export_did_tmp_() -> String {

__export_service()

}

```

This will take every function annotated with #[candid\_method()] and add it to the candid output. Works like a charm for fast prototyping, and can be later modified to act as a test for your “manually curated” did file.

---

<div class="post-metadata">

**Author:** ![ildefons](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ildefons/32/2638_2.png) [@ildefons](https://forum.dfinity.org/u/ildefons)\
**Post date:** [April 23, 2022, 3:40pm UTC](https://forum.dfinity.org/t/question-about-defining-an-update-method-with-rust/12350/3 "2022-04-23T15:40:23Z")

</div>

This is an awesome solution to my question!  
Would it be possible to read the canister code where you define the static variable RUNTIME\_STATE and contain more complete examples of how to use the “#[candid\_method” macro?

---

<div class="post-metadata">

**Author:** ![GLdev](https://avatars.discourse-cdn.com/v4/letter/g/4bbf92/32.png) [@GLdev](https://forum.dfinity.org/u/GLdev)\
**Post date:** [April 23, 2022, 4:14pm UTC](https://forum.dfinity.org/t/question-about-defining-an-update-method-with-rust/12350/4 "2022-04-23T16:14:22Z")

</div>

Sure, I just pushed a “quickstart” project that I use to test things out. Check it out [here](https://github.com/GLicDEV/ic-quickstart)

---

<div class="post-metadata">

**Author:** ![dymayday](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/dymayday/32/4935_2.png) [@dymayday](https://forum.dfinity.org/u/dymayday)\
**Post date:** [May 2, 2022, 7:45pm UTC](https://forum.dfinity.org/t/question-about-defining-an-update-method-with-rust/12350/5 "2022-05-02T19:45:23Z")

</div>

@GLdev Hey, I cannot make this`, guard = "is_controller")]` you mentioned work, and I cannot find any use of it in your repo as well, could you give more details about it please ?

---

<div class="post-metadata">

**Author:** ![GLdev](https://avatars.discourse-cdn.com/v4/letter/g/4bbf92/32.png) [@GLdev](https://forum.dfinity.org/u/GLdev)\
**Post date:** [May 3, 2022, 4:15am UTC](https://forum.dfinity.org/t/question-about-defining-an-update-method-with-rust/12350/6 "2022-05-03T04:15:56Z")

</div>

For that to work you’ll need to first get your canister’s controllers, have them in a vec or something and then iterate over that vec to check if the caller is on that list.

There are a few ways to get a canister’s controllers, but surprisingly not an ic\_cdk simple API to do so. The easiest is to first add the return from “ic\_cdk::api::caller()” inside your init() function. That will give you the dfx principal (with the latest dfx deploy). With that added you could have a manual function, guarded by the first controller, to add more controllers.

An advanced method is to call the management canister asking for a status of your own canister\_id, and that will return ALL the controllers of a canister.

Check my [post](https://forum.dfinity.org/t/getting-a-canisters-controllers-from-rust/11873) here. Also you can check my [quickstart scaling repo](https://github.com/GLicDEV/quickstart_scaling) for the implementation of the above is\_controller. Note that how you add & keep track of controllers is up to you, until we get a simple ic\_cdk api.
