# Automatic generation of Candid from Rust

**URL:** <https://forum.dfinity.org/t/automatic-generation-of-candid-from-rust/14627>\
**Category:** Rust\
**Created:** [July 30, 2022, 11:09am UTC](https://forum.dfinity.org/t/automatic-generation-of-candid-from-rust/14627 "2022-07-30T11:09:04Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![oss](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/oss/32/5318_2.png) [@oss](https://forum.dfinity.org/u/oss)\
**Post date:** [July 30, 2022, 2:28pm UTC](https://forum.dfinity.org/t/automatic-generation-of-candid-from-rust/14627/2 "2022-07-30T14:28:05Z")

</div>

Hey yeah we’ve been able to generate candid from rust for a while now. Not sure why it’s still kinda hidden from documentation. All the psychedelic projects use it ! You just need to add the CandidType derive, and the  
`candid_service(query/update/init)` macro to all the methods and types that need to be exported. Then, the export\_service!() macro will generate function that returns the candid from rust, which can be run differently depending on the project setup

For projects using a main.rs: You can add a main function only run when target is not wasm32. Then, running `cargo run` would print the candid, which can be easily piped to a file (`cargo run > candid.did` ). This works well with mono canister architechtures.  
Example: [nft-marketplace/main.rs at develop · Psychedelic/nft-marketplace · GitHub](https://github.com/Psychedelic/nft-marketplace/blob/develop/marketplace/src/main.rs#L1223-L1236)

for projects using cdylib/lib.rs: You can make a rust test called save\_candid, which would save the candid to a file in the project root when running `cargo test`. This works really well for multi canister architectures.  
Example: [cap/lib.rs at main · Psychedelic/cap · GitHub](https://github.com/Psychedelic/cap/blob/main/canisters/bucket/src/lib.rs#L110-L130)

Personally, I’ve been moving solely to the lib.rs/rust test flow, to allow exporting multiple canisters did interfaces with a single command. If you have other tests as well, you can run only the candid tests using `cargo test save_candid`

* * *

We also plan on making this easy and automatic with the [ic\_kit - Rust](https://docs.rs/ic-kit/latest/ic_kit/), where we’d include the corresponding candid\_method with the `#[update/query/init]` macro automatically

---

_[View the full topic](https://forum.dfinity.org/t/automatic-generation-of-candid-from-rust/14627)._
