Runtime feature flags for Rust — toggle features without recompiling
Sign up at flagbit.anethoth.com to get your SDK key. The free tier includes 1,000 evaluations/day.
cargo add reqwest serde serde_json
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()
}
}
Toggle features at runtime without recompiling — Rust's compile times make this especially valuable.
Roll out new Rust service versions to a percentage of traffic before full deployment.
A/B test performance-critical algorithms (sorting, hashing, compression) in production.
Gradually migrate from one backing service to another with percentage-based rollouts.
Define flags in your FlagBit dashboard with targeting rules and rollout percentages.
Call the evaluate endpoint from your Rust app with user context for targeted rollouts.
Enable, disable, or adjust rollouts in real-time. No redeployment needed.
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.
Free tier includes 1 project, 10 flags, and 1,000 evaluations/day. No credit card required.
Get Your Free API Key