Skip to main content

Rust Quickstart

Get a SpacetimeDB Rust app running in under 5 minutes.

Prerequisites


Create your project

Run the spacetime dev command to create a new project with a Rust SpacetimeDB module.

This will start the local SpacetimeDB server, compile and publish your module, and generate Rust client bindings.

spacetime dev --template basic-rust my-spacetime-app
Explore the project structure

Your project contains both server and client code.

Edit spacetimedb/src/lib.rs to add tables and reducers. Use the generated bindings in client/src/module_bindings/ to build your client.

my-spacetime-app/
├── spacetimedb/ # Your SpacetimeDB module
│ ├── Cargo.toml
│ └── src/
│ └── lib.rs # Server-side logic
├── client/ # Client application
│ ├── Cargo.toml
│ └── src/
│ ├── main.rs
│ └── module_bindings/ # Auto-generated types
└── README.md
Understand tables and reducers

Open spacetimedb/src/lib.rs to see the module code. The template includes a Person table and two reducers: add to insert a person, and say_hello to greet everyone.

Tables store your data. Reducers are functions that modify data — they're the only way to write to the database.

use spacetimedb::{ReducerContext, Table};

#[spacetimedb::table(name = person, public)]
pub struct Person {
    name: String,
}

#[spacetimedb::reducer]
pub fn add(ctx: &ReducerContext, name: String) {
    ctx.db.person().insert(Person { name });
}

#[spacetimedb::reducer]
pub fn say_hello(ctx: &ReducerContext) {
    for person in ctx.db.person().iter() {
        log::info!("Hello, {}!", person.name);
    }
    log::info!("Hello, World!");
}
Test with the CLI

Use the SpacetimeDB CLI to call reducers and query your data directly.

# Call the add reducer to insert a person
spacetime call my-spacetime-app add Alice

# Query the person table
spacetime sql my-spacetime-app "SELECT * FROM person"
 name
---------
 "Alice"

# Call say_hello to greet everyone
spacetime call my-spacetime-app say_hello

# View the module logs
spacetime logs my-spacetime-app
2025-01-13T12:00:00.000000Z  INFO: Hello, Alice!
2025-01-13T12:00:00.000000Z  INFO: Hello, World!

Next steps