Rust and Motoko Interop

Since Rust and Motoko both compile to Wasm, they should be able to interoperate.

What’s the story on that interoperation currently? I imagine Motoko → Rust interop might be more mature than Rust → Motoko interop, since Rust is much more mature and probably has better FFI or other mechanisms.

I’m currently writing a GraphQL library in Rust for the IC, but I would love for Motoko developers to be able to use it (and I’d like to avoid rewriting it if possible, especially since I’m using quite a few Rust libraries that implement a lot of the GraphQL functionality, all of which would have to be rewritten in Motoko).

Yep, they can interop via Candid. You can check this CDK example: cdk-rs/examples/counter/src at next · dfinity/cdk-rs · GitHub, and the doc: How to :: Internet Computer


Thank you, though I’m discussing interop at the language/binary level, not at the canister level. I am writing a library in Rust that the user creates their own structs and functions for, to define GraphQL types and resolvers. Those types and resolvers are handed to an already-existing GraphQL library (written in Rust) that then allows for query execution.

As designed, the user must use Rust if they want to add their own GraphQL types and resolvers. I want the user to also be able to create GraphQL types and resolvers in Motoko. The types I might be able to deal with already, but the resolvers are functions and I do not believe are easily serializable. I am hoping there is a way to import the Rust library’s Wasm binary into Motoko so that the user can then pass in their GraphQL types and resolvers written in Motoko.

This functionality is not something inter-canister calls can achieve, the canister itself needs access to the Rust binary so that it can resolve GraphQL queries. I want both canisters written in Rust and Motoko to be able to resolve GraphQL queries directly within themselves.

I should clarify, perhaps inter-canister calls could achieve this, but I think it could create unnecessary canister complexity and runtime latency, when instead everything could be done elegantly from within one canister, if binaries can easily be imported from Rust into Motoko

You are right, there is no FFI support at the moment. But we can do this as inter-canister calls by providing callback functions as resolvers. You can implement the resolver functions in Motoko and send it to Rust as a function reference. The limitation is that the resolver function cannot be polymorphic, and it adds some latency.