# When need to use \`ManualReply \`?

**URL:** <https://forum.dfinity.org/t/when-need-to-use-manualreply/13194>\
**Category:** Getting Started\
**Tags:** Discussing\
**Created:** [May 22, 2022, 7:26am UTC](https://forum.dfinity.org/t/when-need-to-use-manualreply/13194 "2022-05-22T07:26:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zshuaibin](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/zshuaibin/32/5580_2.png) [@zshuaibin](https://forum.dfinity.org/u/zshuaibin)\
**Post date:** [May 22, 2022, 7:26am UTC](https://forum.dfinity.org/t/when-need-to-use-manualreply/13194/1 "2022-05-22T07:26:03Z")

</div>

I was following the sample code of `rust-profile`，i was confused by using `ManualReply`.

 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/6/6a9ca70570d0a0126a4151ed40aa54008bc3aa11.png)

I’ve checked the docs here：  
[query in ic\_cdk\_macros - Rust (docs.rs)](https://docs.rs/ic-cdk-macros/latest/ic_cdk_macros/attr.query.html)

 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/8/8de764047fd3b7462febf788a38f1d3f420b6880.png)

so，when need to use `ManualReply`?

Thank you ~~

---

<div class="post-metadata">

**Author:** ![AdamS](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/adams/32/5248_2.png) [@AdamS](https://forum.dfinity.org/u/AdamS)\
**Post date:** [May 23, 2022, 2:20pm UTC](https://forum.dfinity.org/t/when-need-to-use-manualreply/13194/2 "2022-05-23T14:20:10Z")

</div>

In the Rust abstraction over the system interface, functions take parameters and produce return values. In the system interface, functions are `() -> ()`, take input by calling `ic0.msg_arg_data_*`, and produce output by calling `ic0.reply`. Sometimes these two models of the world conflict.

The function you have posted is one that ‘returns’ `Profile` in Candid-encoded form. The convenient abstraction would be to return the `Profile` struct from the function - _except_ `Profile` contains owned non-`Copy` data. To return it by value would require cloning it, and there is no lifetime that it could be returned under by reference because it is obtained through `thread_local!`. The canister would like to not waste cycles with an allocation (allocations are cheap, but it’s an example for when your operation _isn’t_ cheap).

But ownership and lifetimes are irrelevant to the underlying operation - it’s just getting serialized and passed to `ic0.reply`, which does not require ownership. If you could just ‘return’ your data that way, there are no lifetime problems, and you can pass it to the function by reference just fine. Thus, the `manual_reply = true` flag on the export macros allows you to reply yourself to skip the entire problem. (This is necessary because otherwise the export macro would call `ic0.reply` itself, and calling `ic0.reply` twice traps.)

`ManualReply` is an abstraction over manually calling `ic0.reply`. It is a struct that contains no data - it only exists to provide `CandidType::ty`, so that declaring it as a return type allows you to still auto-generate Candid bindings. When you construct it with parameters, it immediately calls `ic0.reply` regardless of whether it’s returned or not. Saying

```nohighlight
return ManualReply::one(Some(p));

```

is equivalent to saying

```nohighlight
call::reply((Some(p),));
return;

```

except that declaring it as your return type ensures you cannot accidentally return a value without actually exiting the function. Use of this type is not required when using `manual_reply = true` - you can use `ic_cdk::api::call::reply` if you want to.

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [May 26, 2022, 10:58pm UTC](https://forum.dfinity.org/t/when-need-to-use-manualreply/13194/3 "2022-05-26T22:58:18Z")

</div>

I hadn’t seen Adam’s reply but came across this motivating example and quickly understood why it’s needed:

> <https://github.com/dfinity/examples/blob/8381ca44a172e5aeed7afdf82261c7f3233aac58/rust/dip721-nft-container/src/http.rs#L33-L40>

---

<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:** [October 27, 2022, 8:57am UTC](https://forum.dfinity.org/t/when-need-to-use-manualreply/13194/4 "2022-10-27T08:57:49Z")

</div>

Very good example, just that I had a problem after looking closely at the comments:

1. Why do you say cdk 0.5 makes this unnecessary?
2. Why are `headers: HashMap<String, String>` not `headers: Vec<(String, String)>`

> <https://github.com/dfinity/examples/blob/master/rust/dip721-nft-container/src/http.rs#L22>

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [October 27, 2022, 3:40pm UTC](https://forum.dfinity.org/t/when-need-to-use-manualreply/13194/5 "2022-10-27T15:40:21Z")

</div>

1. I think that’s referring to `ic::cdk_setup`, `call::arg_data`, and `call::reply`.

2. I think because their Candid representation is the same.
