How to create a query module in Rust
To create a query module in Rust for Memgraph, you need to compile your code
into a shared library (.so
file) that Memgraph can load dynamically at
runtime.
Development environment options
You have two main ways to set up your Rust development environment:
-
Using your own environment
You can compile Rust query modules on your local machine or server, but you’ll need to manually install Rust tooling and any necessary dependencies (likecargo
and system libraries). -
Using the MAGE environment
The Memgraph MAGE development Docker image (with the-dev
tag) contains all required dependencies and provides an isolated environment ideal for development.
See the quickstart to custom query module in Rust for a full guide.
How the Rust API works
The Rust API is a wrapper around Memgraph’s C API, providing a more ergonomic and memory-safe interface while maintaining high performance. Query modules written in Rust are compiled to shared libraries and loaded by Memgraph at startup.
- The API includes wrappers for working with graph data, procedures, results, errors, and more.
- You can define custom procedures using macros like
init_module!
,define_procedure!
, andclose_module!
. - Exception handling in Rust (with
Result
types) ensures better stability, but unhandledpanic!
calls can still crash Memgraph.
Full reference: Rust API documentation.
Quickstart summary
To get started quickly:
- Run Memgraph using the MAGE development Docker image.
- Install
cargo
inside the container. - Copy the query module boilerplate and adjust
Cargo.toml
andlib.rs
. - Build your module using
python3 setup build -p /usr/lib/memgraph/query_modules/
. - Load the module in Memgraph with
CALL mg.load_all();
.
Follow the quickstart guide for detailed instructions.