# Using Rust packages from a Motoko canister

**URL:** <https://forum.dfinity.org/t/using-rust-packages-from-a-motoko-canister/30055>\
**Category:** Rust\
**Created:** [April 26, 2024, 8:04pm UTC](https://forum.dfinity.org/t/using-rust-packages-from-a-motoko-canister/30055 "2024-04-26T20:04:55Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![timk11](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/timk11/32/9489_2.png) [@timk11](https://forum.dfinity.org/u/timk11)\
**Post date:** [January 31, 2025, 8:33am UTC](https://forum.dfinity.org/t/using-rust-packages-from-a-motoko-canister/30055/6 "2025-01-31T08:33:26Z")

</div>

I’ve put together a minimal example of using a Rust crate in a Motoko canister in this repo - [zkTrainer](https://github.com/timk11/zk-trainer/tree/32920ae17fecd391afc162a8d6dd27cc0ca074f1).

This commit is the very start of a developer grant-funded project (outlined [here](https://forum.dfinity.org/t/zktrainer-a-machine-learning-trainer-for-private-datasets-using-zero-knowledge-proofs/37914)) but I’ve used it to create a framework that I can build upon further and to share the details here for the benefit of anyone who might come across this thread while searching for ways to go about this.

The steps I used to set this up are as follows:

- check [prerequisites](https://internetcomputer.org/docs/current/developer-docs/backend/rust/quickstart#prerequisites) and run the usual commands to start a new `dfx` project  
&nbsp;&nbsp;`dfx start --clean --background`  
&nbsp;&nbsp;`dfx new my-project --type=rust`  
&nbsp;&nbsp;`cd my-project`  
&nbsp;&nbsp;`code .` (to open VSCode)
- rename `my-project-backend` to `wrapper` (or whatever name you prefer) using find & replace, and rename the directory accordingly
- (also search for the same text with underscore instead of hyphen)
- separately start a dfx [Motoko project](https://internetcomputer.org/docs/current/motoko/main/getting-started/quickstart#creating-a-new-project)
- copy the directory containing `main.mo` into `src` in `my-project`
- if renaming the directory, replace the name throughout the project
- add details into [`dfx.json`](https://github.com/timk11/zk-trainer/blob/32920ae17fecd391afc162a8d6dd27cc0ca074f1/dfx.json#L8-L14) file
- copy `Cargo.toml` file from Rust backend directory into Motoko backend directory ([here](https://github.com/timk11/zk-trainer/blob/32920ae17fecd391afc162a8d6dd27cc0ca074f1/src/zk-trainer-backend/Cargo.toml)) and edit package name
- copy `.did` file from Rust backend directory into Motoko backend directory ([here](https://github.com/timk11/zk-trainer/blob/32920ae17fecd391afc162a8d6dd27cc0ca074f1/src/zk-trainer-backend/zk-trainer-backend.did)) and edit file name
- from the project root folder run  
&nbsp;&nbsp;`cargo add <crate-name>`  
&nbsp;&nbsp;`cargo build --target wasm32-unknown-unknown --release`

I then wrote the code for the [Rust wrapper](https://github.com/timk11/zk-trainer/blob/32920ae17fecd391afc162a8d6dd27cc0ca074f1/src/wrapper/src/lib.rs) and [Motoko backend](https://github.com/timk11/zk-trainer/blob/32920ae17fecd391afc162a8d6dd27cc0ca074f1/src/zk-trainer-backend/main.mo) as shown in the links so as to be able to wrap a single type and method from the [winterfell](https://crates.io/crates/winterfell) crate and use it in a Motoko canister.

This might not be the most efficient way to achieve this (and doesn’t use Canpack) but it does produce a working example. Happy to hear if anyone has suggestions for improvement.

---

_[View the full topic](https://forum.dfinity.org/t/using-rust-packages-from-a-motoko-canister/30055)._
