Skip to content

Rust SDK Guide

This guide covers adding Liteguard to a Rust application.


Prerequisites

  • A Project Client Token from your Liteguard project. Follow the Getting Started guide through the Copy a Project Client Token step if you do not have one yet.
  • Rust 1.75 or later.
  • A Tokio async runtime.

Add the dependency

toml
[dependencies]
liteguard = "0.5"
tokio = { version = "1", features = ["full"] }

Initialize the client

Client::init is async and fetches the initial bundle before returning.

rust
use liteguard::{Client, ClientOptions};
use std::env;

#[tokio::main]
async fn main() {
    let client = Client::init(
        &env::var("LITEGUARD_TOKEN").expect("LITEGUARD_TOKEN not set"),
        ClientOptions {
            environment: Some(env::var("LITEGUARD_ENV").unwrap_or_default()),
            ..Default::default()
        },
    )
    .await
    .expect("Failed to initialize Liteguard client");

    // ...

    client.shutdown().await;
}

Do not commit your token to source control. Read it from environment variables or a secrets manager.


Evaluate a guard

scope.is_open(guard_name) reads from the locally cached bundle. No network call occurs per check.

Create a scope first, then evaluate against it:

rust
use liteguard::Properties;

let scope = client.create_scope(Properties::new());

if scope.is_open("payments.checkout") {
    // guarded code path
}

Passing properties

Build a Properties value with the caller's attributes:

rust
let scope = client.create_scope(
    Properties::new()
        .set("user_id", &user_id)
        .set("plan", &plan),
);

if scope.is_open("payments.checkout") {
    // guarded code path
}

Reusing a scope

Create one scope per request and call is_open multiple times:

rust
let scope = client.create_scope(
    Properties::new()
        .set("user_id", &user_id)
        .set("plan", &plan),
);

if scope.is_open("payments.checkout") {
    // ...
}

if scope.is_open("billing.invoice_download") {
    // ...
}

Advanced evaluation options

Use is_open_with_options when you need to pass per-call overrides:

rust
use liteguard::Options;

if scope.is_open_with_options("payments.checkout", &Options::default()) {
    // ...
}

Shut down cleanly

shutdown() is async and flushes buffered telemetry before returning:

rust
client.shutdown().await;

Pair it with a SIGTERM handler:

rust
tokio::select! {
    _ = run_server(&client) => {}
    _ = tokio::signal::ctrl_c() => {
        client.shutdown().await;
    }
}

Verify in Liteguard

After calling is_open at least once, open the Guards tab and confirm your guard appears. See Your First Guard to configure its behavior.


Next steps