FlagKit Rust SDK
Official Rust SDK for FlagKit feature flag service.
Requirements
- Rust 1.70 or later
- Tokio runtime
Installation
Add to your Cargo.toml:
[]
= "1.0"
= { = "1.0", = ["rt-multi-thread", "macros"] }
Features
- Type-safe evaluation - Boolean, string, number, and JSON flag types
- Local caching - Fast evaluations with configurable TTL and optional encryption
- Background polling - Automatic flag updates with async runtime
- Event tracking - Analytics with batching and crash-resilient persistence
- Resilient - Circuit breaker, retry with exponential backoff, offline support
- Thread-safe - Safe for concurrent use across threads
- Security - PII detection, request signing, bootstrap verification, cache encryption
Quick Start
use ;
async
Configuration
use ;
use Duration;
let options = builder
.polling_interval
.cache_ttl
.max_cache_size
.cache_enabled
.event_batch_size
.event_flush_interval
.events_enabled
.timeout
.retry_attempts
.build;
initialize?;
Security features such as PII detection, request signing, bootstrap verification, cache encryption, evaluation jitter, and error sanitization are also available as configuration options.
Evaluation Context
Provide context for targeting rules:
use ;
use HashMap;
// Using builder pattern
let context = new
.user_id
.attribute
.attribute
.attribute
.build;
let result = evaluate;
// Using fluent methods
let context = with_user_id
.attribute
.attribute;
Flag Evaluation
Basic Evaluation
// Boolean flags
let enabled = get_boolean_value;
// String flags
let variant = get_string_value;
// Number flags
let limit = get_number_value;
let count = get_int_value;
// JSON flags
let config = get_json_value;
Detailed Evaluation
let result = evaluate;
println!;
println!;
println!;
println!;
println!;
Async Evaluation
// Server-side evaluation with full context
let result = instance
.evaluate_async
.await;
User Identification
use HashMap;
use ;
// Identify user with attributes
let mut attributes = new;
attributes.insert;
attributes.insert;
identify;
// Update context
set_context;
// Clear context
clear_context;
Bootstrap Data
Initialize with local flag data for instant evaluation:
use HashMap;
use json;
let mut bootstrap = new;
bootstrap.insert;
bootstrap.insert;
bootstrap.insert;
let options = builder
.bootstrap
.build;
initialize?;
// Flags available immediately from bootstrap
Error Handling
use ;
Thread Safety
The SDK is thread-safe and can be used from multiple threads:
use thread;
use FlagKit;
let handles: =
.map
.collect;
for handle in handles
API Reference
FlagKit (Static Factory)
| Method | Description |
|---|---|
initialize(options) |
Initialize SDK with options |
instance() |
Get client instance |
is_initialized() |
Check if SDK is initialized |
close() |
Close SDK |
identify(user_id, attributes) |
Set user context |
set_context(context) |
Set evaluation context |
clear_context() |
Clear evaluation context |
evaluate(flag_key, context) |
Evaluate a flag |
get_boolean_value(...) |
Get boolean flag value |
get_string_value(...) |
Get string flag value |
get_number_value(...) |
Get number flag value |
get_int_value(...) |
Get integer flag value |
get_json_value(...) |
Get JSON flag value |
get_all_flags() |
Get all cached flags |
FlagKitOptions
| Property | Default | Description |
|---|---|---|
api_key |
(required) | API key for authentication |
polling_interval |
30s | Polling interval |
cache_ttl |
5min | Cache time-to-live |
max_cache_size |
1000 | Maximum cache entries |
cache_enabled |
true | Enable caching |
event_batch_size |
10 | Events per batch |
event_flush_interval |
30s | Event flush interval |
events_enabled |
true | Enable event tracking |
timeout |
10s | HTTP timeout |
retry_attempts |
3 | Max retry attempts |
bootstrap |
None | Initial flag data |
local_port |
None | Local development server port (uses http://localhost:{port}/api/v1) |
Local Development
When running FlagKit locally, use the local_port option to connect to the local development server:
let options = builder
.local_port // Uses http://localhost:8200/api/v1
.build;
initialize?;
License
MIT License - see LICENSE file for details.