GrowthBook Rust SDK
[!NOTE] This repo was originally developed by the community and later adopted by GrowthBook.
The official GrowthBook SDK for Rust. This crate provides an easy way to integrate feature flagging and experimentation into your Rust applications.
Installation
Add this to your Cargo.toml:
[]
= "0.0.1"
Quick Usage
Initialization
Use the GrowthBookClientBuilder to create a client instance. This supports auto-refreshing features, caching, and callbacks.
use GrowthBookClientBuilder;
use Duration;
async
Checking Features
You can check if a feature is enabled or get its value.
// Simple check
if client.is_on
// Get typed value
let value = client.feature_result.?;
Context & Attributes
You can set global attributes that apply to all evaluations, and override them per-check.
use HashMap;
use ;
// Global attributes
let mut global_attrs = new;
global_attrs.insert;
let client = new
.api_url
.client_key
.attributes
.build
.await?;
// Per-check attributes (merged with global)
let mut user_attrs = Vecnew;
user_attrs.push;
if client.is_on
Tracking Callbacks
You can subscribe to events for tracking and analytics.
let client = new
// ...
.on_feature_usage
.on_experiment_viewed
.build
.await?;
Configuration
The SDK can also be configured via environment variables if not explicitly set in the builder:
| Env Var | Description |
|---|---|
| GB_HTTP_CLIENT_TIMEOUT | Timeout for HTTP requests. Default: 10s |
| GB_UPDATE_INTERVAL | Interval for auto-refresh (if enabled). Default: 60s |
| GB_URL | GrowthBook API URL |
| GB_SDK_KEY | GrowthBook SDK Key |
Refreshing features & Caching
The SDK supports automated feature updates via a background task. This is enabled by default when using auto_refresh(true) in the builder.
- Caching: Features are cached in memory by default. You can configure the TTL using
.ttl(Duration::from_secs(60)). - Background Sync: When
auto_refreshis enabled, a background task periodically fetches features from the API and updates the cache. - On Refresh Callback: You can listen for updates using
.add_on_refresh(...).
Manual Feature Management
If you prefer to manage feature updates manually or want to start with a specific set of features (e.g., from a file or another source), you can disable auto-refresh and provide initial features.
use json;
let features_json = json!;
let client = new
.api_url
.client_key
.auto_refresh // Disable background sync
.features_json? // Set initial features
.build
.await?;
// You can manually refresh features later
client.refresh.await;
Encrypted Features
If you are using encrypted features, you can provide the decryption key to the builder.
let client = new
.api_url
.client_key
.decryption_key
.build
.await?;