Rust SDK
Use Flipswitch with Rust via OpenFeature OFREP
Flipswitch works with Rust applications through the OpenFeature SDK and its native OFREP provider โ no custom Flipswitch package needed.
GitHub: open-feature/rust-sdk ยท open-feature/rust-sdk-contrib
This integration uses the OpenFeature Remote Evaluation Protocol (OFREP), which evaluates flags via polling. Real-time SSE streaming is available in official Flipswitch SDKs.
Requirements
- Rust 1.75+
- Tokio runtime
Installation
cargo add open-feature open-feature-ofrepQuick Start
use open_feature::{OpenFeature, EvaluationContext};
use open_feature_ofrep::OFREPProvider;
let provider = OFREPProvider::new(
"https://api.flipswitch.io",
Some("YOUR_API_KEY"),
);
let mut api = OpenFeature::singleton_mut().await;
api.set_provider(provider).await;
let client = api.create_client();
let dark_mode = client.get_bool_value("dark-mode", Some(&context), None).await?;Evaluation Context
Pass user attributes for targeting:
use open_feature::EvaluationContext;
use std::collections::HashMap;
let mut attributes = HashMap::new();
attributes.insert("email".to_string(), "user@example.com".into());
attributes.insert("plan".to_string(), "premium".into());
attributes.insert("country".to_string(), "SE".into());
let context = EvaluationContext {
targeting_key: Some("user-123".to_string()),
custom_fields: attributes,
};
let show_feature = client.get_bool_value("new-feature", Some(&context), None).await?;Detailed Evaluation
Get full evaluation details including variant and reason:
let details = client.get_bool_details("feature-flag", Some(&context), None).await?;
println!("Value: {}", details.value);
println!("Variant: {:?}", details.variant);
println!("Reason: {:?}", details.reason);Axum Integration
use axum::{extract::Query, routing::get, Json, Router};
use open_feature::OpenFeature;
use serde::{Deserialize, Serialize};
#[derive(Deserialize)]
struct FeatureQuery {
user_id: String,
}
#[derive(Serialize)]
struct FeatureResponse {
feature_enabled: bool,
}
async fn feature_handler(Query(params): Query<FeatureQuery>) -> Json<FeatureResponse> {
let api = OpenFeature::singleton().await;
let client = api.create_client();
let context = EvaluationContext {
targeting_key: Some(params.user_id),
custom_fields: Default::default(),
};
let enabled = client
.get_bool_value("new-feature", Some(&context), None)
.await
.unwrap_or(false);
Json(FeatureResponse { feature_enabled: enabled })
}
#[tokio::main]
async fn main() {
// ... provider setup ...
let app = Router::new().route("/feature", get(feature_handler));
let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await.unwrap();
axum::serve(listener, app).await.unwrap();
}Direct OFREP API
You can also call the OFREP endpoints directly via HTTP โ see the OFREP protocol reference for details.