# Rust Create Canister / Inter-Canister Calls

**URL:** <https://forum.dfinity.org/t/rust-create-canister-inter-canister-calls/2016>\
**Category:** Developers\
**Created:** [February 20, 2021, 4:22pm UTC](https://forum.dfinity.org/t/rust-create-canister-inter-canister-calls/2016 "2021-02-20T16:22:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steve](https://avatars.discourse-cdn.com/v4/letter/s/f19dbf/32.png) [@Steve](https://forum.dfinity.org/u/Steve)\
**Post date:** [February 20, 2021, 4:22pm UTC](https://forum.dfinity.org/t/rust-create-canister-inter-canister-calls/2016/1 "2021-02-20T16:22:34Z")

</div>

Does anyone know how to use the Rust CDK call function?

> <https://github.com/dfinity/cdk-rs/blob/ee145313fa9feae4ae0ab7602d8fca39374c3fb8/src/ic-cdk/src/api/call.rs#L140>

I’m trying to call the management canister to create a new canister from wasm. It looks like I just need to implement the ArgumentEncoder trait on my struct, but it feels a bit laborious to do that for all my types.

> <https://github.com/dfinity/candid/blob/ea2c72f57e4b1cde99fad3b61308c0e979a65e3a/rust/candid/src/ser.rs#L399>

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [February 20, 2021, 5:58pm UTC](https://forum.dfinity.org/t/rust-create-canister-inter-canister-calls/2016/2 "2021-02-20T17:58:20Z")

</div>

`ArgumentEncoder` is implemented for all tuple types `(T1, T2, ...)`, where `Ti: CandidType`. If you have a custom struct, you only need to `#[derive(CandidType, Deserialize)]`, then pass in `args` as a tuple, for example `(your_struct, 42, "Text")`.

---

<div class="post-metadata">

**Author:** ![Steve](https://avatars.discourse-cdn.com/v4/letter/s/f19dbf/32.png) [@Steve](https://forum.dfinity.org/u/Steve)\
**Post date:** [February 22, 2021, 3:59pm UTC](https://forum.dfinity.org/t/rust-create-canister-inter-canister-calls/2016/3 "2021-02-22T15:59:30Z")

</div>

Hey @chenyan - I’m having issues with `ArgumentDecoder` now. If its not too much hassle, could share an example of just calling a canister, and getting the result?

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [February 22, 2021, 5:41pm UTC](https://forum.dfinity.org/t/rust-create-canister-inter-canister-calls/2016/4 "2021-02-22T17:41:26Z")

</div>

```auto
let (first_result, second_result) : (first_result_type, second_result_type) = 
  api::call::call(canister_id, "method", (first_arg, second_arg)).await?;

```

Both argument and return types are tuples. The type annotation on return type is required, as it cannot be inferred by Rust.

---

<div class="post-metadata">

**Author:** ![Steve](https://avatars.discourse-cdn.com/v4/letter/s/f19dbf/32.png) [@Steve](https://forum.dfinity.org/u/Steve)\
**Post date:** [February 22, 2021, 6:18pm UTC](https://forum.dfinity.org/t/rust-create-canister-inter-canister-calls/2016/5 "2021-02-22T18:18:14Z")

</div>

Thanks, let me give that a shot! Just so I make sure I understand

`(first_result_type, second_result_type) ` → `(candid::Nat, candid::Principle)`

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [February 22, 2021, 7:15pm UTC](https://forum.dfinity.org/t/rust-create-canister-inter-canister-calls/2016/6 "2021-02-22T19:15:49Z")

</div>

That’s the return type of your method. If you are not sure, can you post the did file for that method?

---

<div class="post-metadata">

**Author:** ![Steve](https://avatars.discourse-cdn.com/v4/letter/s/f19dbf/32.png) [@Steve](https://forum.dfinity.org/u/Steve)\
**Post date:** [February 22, 2021, 10:47pm UTC](https://forum.dfinity.org/t/rust-create-canister-inter-canister-calls/2016/7 "2021-02-22T22:47:52Z")

</div>

@chenyan - sorry for the confusion here is a full example

```auto
let canister : (candid::Principal) = ic_cdk::api::call(candid::Principal::management_canister(), "create_canister", ()).await?;

```

Yields the following error:

```auto
the trait `for<'a> ArgumentDecoder<'a>` is not implemented for `ic_types::Principal`

```

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [February 22, 2021, 11:26pm UTC](https://forum.dfinity.org/t/rust-create-canister-inter-canister-calls/2016/8 "2021-02-22T23:26:40Z")

</div>

The return type is a tuple, even if it has only one value, so this will work:  
`let (canister,) : (candid::Principal,)`

---

<div class="post-metadata">

**Author:** ![Steve](https://avatars.discourse-cdn.com/v4/letter/s/f19dbf/32.png) [@Steve](https://forum.dfinity.org/u/Steve)\
**Post date:** [February 23, 2021, 12:00am UTC](https://forum.dfinity.org/t/rust-create-canister-inter-canister-calls/2016/9 "2021-02-23T00:00:10Z")

</div>

🤦‍♂️ DOH! Yes its working now. Thank you!!

---

<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 26, 2021, 11:55pm UTC](https://forum.dfinity.org/t/rust-create-canister-inter-canister-calls/2016/10 "2021-05-26T23:55:05Z")

</div>

Hey there!

I want to call canister methods without knowing their interface at compile time and I have a code like this

```auto
fn do_call(idl_args: IDLArgs, canister_id: Principal, method: &str) {
  let bytes = idl_args.to_bytes()?;

  let result = call_raw(
    canister_id,
    method,
    bytes,
    0,
  )
    .await;
}

```

It successfully executes inside a canister but the actual `call` is not performed. What am I doing wrong?

UPD: `canister_id`, `method` and `idl_args` are 100% fit in the remote interface.

---

<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 27, 2021, 9:05am UTC](https://forum.dfinity.org/t/rust-create-canister-inter-canister-calls/2016/11 "2021-05-27T09:05:51Z")

</div>

@chenyan Please help me ☹

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [May 27, 2021, 4:48pm UTC](https://forum.dfinity.org/t/rust-create-canister-inter-canister-calls/2016/12 "2021-05-27T16:48:02Z")

</div>

`call_raw` returns `Future<Result<...>>`, so you will need `.await?`.

---

<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 27, 2021, 4:50pm UTC](https://forum.dfinity.org/t/rust-create-canister-inter-canister-calls/2016/13 "2021-05-27T16:50:53Z")

</div>

Yes, it’s there. I did not include it into the snippet, sorry. There is also a check if `result` is `Err` and it is not.

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [May 27, 2021, 4:53pm UTC](https://forum.dfinity.org/t/rust-create-canister-inter-canister-calls/2016/14 "2021-05-27T16:53:05Z")

</div>

I don’t understand the question here. What do you mean by “successfully executes, but the actual call is not performed”? Are you getting the return blob from `call_raw`?

---

<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 27, 2021, 5:01pm UTC](https://forum.dfinity.org/t/rust-create-canister-inter-canister-calls/2016/15 "2021-05-27T17:01:08Z")

</div>

The canister I’m calling to is logging every request it receives and my example request should also change its state (it is an update call).

When the second canister executes a code from the snippet, the first canister does not log a request and does not change its state as well. The response the second canister receives from that call is not `Err`, so it looks like everything should be all right, but it’s not.

I was thinking, am I encoding arguments correctly?  
Or another possible issue: maybe the response is not `Err` but it returns an encoded error in the `Ok` variant?

---

<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 27, 2021, 5:02pm UTC](https://forum.dfinity.org/t/rust-create-canister-inter-canister-calls/2016/16 "2021-05-27T17:02:24Z")

</div>

Sorry, I just don’t know how to demonstrate that issue, since there is no error code or something.

Thank you for your patience.

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [May 27, 2021, 5:13pm UTC](https://forum.dfinity.org/t/rust-create-canister-inter-canister-calls/2016/17 "2021-05-27T17:13:01Z")

</div>

You can use `didc decode` to inspect the return blob. Or if you know how many values the function returns, you can decode in Rust via `Decode!(&result, IDLValue, IDLValue, ...)?`

`call_raw` looks good to me, I think the problem might be on the other canister, where it doesn’t actually log every requests. So looking at the return result might help.

---

<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 27, 2021, 5:13pm UTC](https://forum.dfinity.org/t/rust-create-canister-inter-canister-calls/2016/18 "2021-05-27T17:13:37Z")

</div>

Thanks for the help!

---

<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 30, 2021, 2:02pm UTC](https://forum.dfinity.org/t/rust-create-canister-inter-canister-calls/2016/20 "2021-05-30T14:02:44Z")

</div>

How do you spawn a new canister in rust?

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [June 18, 2021, 7:24pm UTC](https://forum.dfinity.org/t/rust-create-canister-inter-canister-calls/2016/21 "2021-06-18T19:24:24Z")

</div>

Here is my function in Motoko:

```auto
public query func test(number: Nat): async Nat {
        return number;
    };

```

Here is my function in Rust:

```auto
async fn test(number: i32) -> Result<i32, sudograph::async_graphql::Error> {
    let call_result: Result<(i32,), _> = ic_cdk::api::call::call(ic_cdk::export::Principal::from_text("ryjl3-tyaaa-aaaaa-aaaba-cai").unwrap(), "customGet", (number)).await;
    return Ok(call_result.unwrap().0);
}

```

I keep getting:

```bash
error[E0277]: the trait bound `i32: ArgumentEncoder` is not satisfied
   --> canisters/graphql/src/graphql.rs:24:157
    |
24 | ...aaba-cai").unwrap(), "customGet", (number)).await;
    | ^^^^^^^^ the trait `ArgumentEncoder` is not implemented for `i32`
    | 
   ::: /home/lastmjs/.cargo/registry/src/github.com-1ecc6299db9ec823/ic-cdk-0.3.0/src/api/call.rs:260:22
    |
260 | pub async fn call<T: ArgumentEncoder, R: for<'a> ArgumentDecoder<'a>>(
    | --------------- required by this bound in `sudograph::ic_cdk::call`

```

If I try just text or numbers as arguments to the call, nothing works. Is there some import I am missing?

[Next page](https://forum.dfinity.org/t/rust-create-canister-inter-canister-calls/2016.md?page=2)
