Rust Integration Guide

Rust Feature Flags

Runtime feature flags for Rust — toggle features without recompiling

Quick Start

1. Get your API key

Sign up at flagbit.anethoth.com to get your SDK key. The free tier includes 1,000 evaluations/day.

2. Install (optional)

cargo add reqwest serde serde_json

3. Evaluate flags

use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Serialize)]
struct FlagRequest {
    flag_key: String,
    context: HashMap<String, String>,
}

#[derive(Deserialize)]
struct FlagResponse {
    value: bool,
}

async fn get_flag(
    client: &Client,
    sdk_key: &str,
    flag_key: &str,
    context: HashMap<String, String>,
) -> bool {
    let resp = client
        .post("https://flagbit.anethoth.com/api/v1/evaluate")
        .header("X-SDK-Key", sdk_key)
        .json(&FlagRequest {
            flag_key: flag_key.to_string(),
            context,
        })
        .send()
        .await;

    match resp {
        Ok(r) => r.json::<FlagResponse>().await.map(|f| f.value).unwrap_or(false),
        Err(_) => false,
    }
}

// Axum handler with feature flags
use axum::{extract::State, response::Json, routing::get, Router};

async fn search_handler(State(state): State<AppState>) -> Json<SearchResult> {
    let mut ctx = HashMap::new();
    ctx.insert("user_id".into(), "123".into());

    if get_flag(&state.http_client, &state.sdk_key, "new-search", ctx).await {
        Json(new_search_engine().await)
    } else {
        Json(legacy_search().await)
    }
}

// Actix-Web middleware
use actix_web::{web, HttpResponse, middleware};

async fn beta_page(data: web::Data<AppState>) -> HttpResponse {
    let enabled = get_flag(&data.client, &data.sdk_key, "beta-page", HashMap::new()).await;
    if enabled {
        HttpResponse::Ok().body("Welcome to the beta!")
    } else {
        HttpResponse::NotFound().finish()
    }
}

Use Cases for Rust

Runtime Toggles

Toggle features at runtime without recompiling — Rust's compile times make this especially valuable.

Canary Deployments

Roll out new Rust service versions to a percentage of traffic before full deployment.

Algorithm Switching

A/B test performance-critical algorithms (sorting, hashing, compression) in production.

Infrastructure Migration

Gradually migrate from one backing service to another with percentage-based rollouts.

How FlagBit Works

1

Create a Flag

Define flags in your FlagBit dashboard with targeting rules and rollout percentages.

2

Evaluate in Code

Call the evaluate endpoint from your Rust app with user context for targeted rollouts.

3

Toggle Instantly

Enable, disable, or adjust rollouts in real-time. No redeployment needed.

FAQ

Cargo features are compile-time. FlagBit flags are runtime — toggle without rebuilding, which matters when Rust builds take minutes.

Use a DashMap or tokio::sync::RwLock with TTL. The moka crate provides a production-ready cache with TTL support.

Yes — the example uses async/await with reqwest. Works with Tokio, async-std, or any async runtime.

FlagBit requires HTTP — it's designed for server-side Rust, not embedded/no_std.

Start using feature flags in Rust

Free tier includes 1 project, 10 flags, and 1,000 evaluations/day. No credit card required.

Get Your Free API Key