Skip to main content

VillageSQL is a drop-in replacement for MySQL with extensions.

All examples in this guide work on VillageSQL. Install Now →
VillageSQL extensions can be written in Rust using the villagesql crate. The SDK handles all FFI marshaling — you write ordinary Rust, and the extension! macro generates the C entry points the server calls at load time.

When to Choose Rust

Rust is the right choice when:
  • You prefer Rust and don’t need a C++ codebase
  • You want memory safety without a garbage collector
  • You’re building a new extension and have no existing C++ dependency
If you have an existing C++ codebase or need the most complete custom-type builder API available today, see Writing C++ Extensions.
The Rust SDK is at version 0.0.1. The API is stable for extension development but the custom-type builder API is still maturing.

Prerequisites

You need:
  • Rust stable toolchain — install at rustup.rs
  • cargo-vsql — the Cargo subcommand for packaging, installing, and testing
Install cargo-vsql from the SDK repo:

Set Up a New Crate

Create a library crate and configure it:
Edit Cargo.toml:
Add manifest.json alongside Cargo.toml:

Write a Function

The Building Extensions in Rust page has the full walkthrough with the ROT-13 example. The short version: implement fn(&[InValue]) -> VdfReturn, register it with func!, and wrap everything in extension!:
For custom types (binary storage, ordering, hashing), see Custom Types in Rust.

Test Your Extension

Write a test in mysql-test/t/basic.test:
Set your VillageSQL build directory, generate expected results, then run:
A passing run prints [ pass ] for each test file.

Install in SQL

Package and install:
Then in SQL:

See also