---
title: "Rust"
description: "Embedding SurrealDB in Rust"
canonical_url: "https://surrealdb.com/docs/integrate/embedding/by-language/rust"
section: "By language"
surrealdb_version: "4.x"
last_modified: "2026-11-18T02:09:16Z"
---

> Full SurrealDB documentation index: https://surrealdb.com/docs/llms.txt

# Rust

Embedding SurrealDB in Rust

SurrealDB can be run as an embedded database within your Rust application, allowing you to use SurrealDB without running a separate server process. This is ideal for desktop applications, testing, local development, and edge computing scenarios.

## Embedded database options

SurrealDB supports multiple types of embedded storage in Rust:

- **In-memory database** (`Mem`) - Fastest performance with data stored in RAM. Perfect for testing, caching, or temporary data. Data is lost when the connection closes.

**SurrealDB 4.x**

- **File-based database** (`SurrealKv`) - Persistent storage on disk using the SurrealKV storage engine. Data persists across connections and application restarts. RocksDB (`RocksDb`) was removed in SurrealDB 4.0, and SurrealKV converts a RocksDB data directory the first time it opens it; see [Moving a RocksDB database to SurrealKV](/docs/operate/self-hosted/deployment-models.md#moving-a-rocksdb-database-to-surrealkv).

**SurrealDB 3.x**

- **File-based database** (`RocksDb` or `SurrealKv`) - Persistent storage on disk using the RocksDB or SurrealKV storage engines. Data persists across connections and application restarts.

## Quick example

```rust
use surrealdb::Surreal;
use surrealdb::engine::local::{Mem, SurrealKv};
use surrealdb::types::SurrealValue;

#[derive(Debug, SurrealValue)]
struct Person {
    name: String,
}

#[tokio::main]
async fn main() -> surrealdb::Result<()> {
    // In-memory database
    let db = Surreal::new::<Mem>(()).await?;
    db.use_ns("main").use_db("main").await?;
    let person: Option<Person> = db
        .create("person")
        .content(Person { name: "John Doe".into() })
        .await?;
    println!("{person:?}");

    // File-based persistent database (SurrealKV)
    let db = Surreal::new::<SurrealKv>("./mydb").await?;
    db.use_ns("main").use_db("main").await?;
    let person: Option<Person> = db
        .create("person")
        .content(Person { name: "Jane Doe".into() })
        .await?;
    println!("{person:?}");
    Ok(())
}
```

## Desktop apps with a web frontend

In a desktop framework such as [Tauri](https://tauri.app), the interface runs in a webview and the application code in Rust. An embedded database runs inside the Rust process and does not listen on a network port, so the [JavaScript SDK](/docs/reference/javascript.md) in the webview has nothing to connect to. There are two ways to give the interface its data:

- **Query from Rust.** Keep the embedded database in the Rust code, and expose the queries the interface needs as Tauri commands, which the webview calls with `invoke`. The database stays private to the application.
- **Run a server.** Start `surreal db start` as a separate process listening on `localhost`, and connect to it from both the Rust code and the JavaScript SDK. Both see the same data, at the cost of a second process to start and stop with the application.

The JavaScript SDK can also [embed its own engine](/docs/integrate/embedding/by-language/javascript.md) in the webview, but that is a separate database from the one in the Rust process.

For complete documentation, installation instructions, examples, best practices, and troubleshooting, see the [Rust SDK embedding guide](/docs/reference/rust/embedding.md).
