The official GrowthBook SDK for Rust. This SDK provides a powerful, type-safe way to integrate feature flagging and A/B testing into your Rust applications with automatic feature refreshing, caching, and tracking callbacks. Rust SDK Resources v0.1.1 growthbook-rustcrates.ioRust example appGet help on SlackDocumentation Index
Fetch the complete documentation index at: https://growthbook-preview.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Requirements
- Rust 1.70.0 or higher (as specified in
rust-toolchain) - Async runtime: Tokio (recommended) or any
async-stdcompatible runtime
Installation
Add this to yourCargo.toml:
Quick Usage
Step 1: Initialize the Client
Step 2: Evaluate Feature Flags
Loading Features and Experiments
The Rust SDK provides multiple ways to load and refresh feature definitions from the GrowthBook API.Built-in Fetching and Auto-Refresh
The recommended approach is to use the builder with auto-refresh enabled:How Auto-Refresh Works
Whenauto_refresh is enabled:
- Features are fetched immediately during
build() - A background task spawns that periodically fetches updates
- The cache is updated automatically without blocking your application
- The refresh task runs until the client is dropped
- Always up-to-date features without manual intervention
- Non-blocking updates in the background
- Configurable refresh intervals
- Automatic retry logic on failures
Manual Refresh
You can manually trigger a feature refresh at any time:Starting with Initial Features
If you want to start with a specific set of features (e.g., from a file or cache) and then enable updates:Disabling Auto-Refresh
For use cases where you want full control (e.g., testing, edge workers, or custom update logic):Refresh Callback
Get notified when features are refreshed (useful for logging and debugging):Configuration via Environment Variables
The SDK supports configuration through environment variables:| Variable | Description | Default |
|---|---|---|
GB_HTTP_CLIENT_TIMEOUT | HTTP request timeout | 10 seconds |
GB_UPDATE_INTERVAL | Auto-refresh interval | 60 seconds |
GB_URL | GrowthBook API URL | - |
GB_SDK_KEY | SDK client key | - |
Encrypted Features
For enhanced security, GrowthBook supports encrypted feature payloads. This prevents sensitive feature configurations and PII data from being exposed in transit or in logs.Setup
- Enable encryption in your GrowthBook SDK Connection settings
- Copy the decryption key shown in the GrowthBook dashboard
- Pass the key to the SDK during initialization
How It Works
- Feature payloads from the API are encrypted using AES-256
- The SDK automatically decrypts them using your decryption key
- Decryption happens transparently - your code doesn’t change
- Invalid keys or corrupted data will cause initialization to fail
Security Best Practices
- Use environment variables or secret management systems (AWS Secrets Manager, HashiCorp Vault)
- Rotate keys regularly
- Use different keys for different environments (dev, staging, production)
- Never commit keys to version control
Error Handling
Attributes
Attributes are used for two main purposes:- Feature targeting - Show different values to different user segments
- Experiment bucketing - Ensure consistent variation assignment
Setting Global Attributes
You can set default attributes that apply to all feature evaluations:Per-Evaluation Attributes
You can override or supplement global attributes on a per-check basis:Attribute Types
The SDK supports all JSON data types as attributes:Common Attribute Patterns
Attribute Merging Behavior
When you provide per-evaluation attributes:- They are merged with global attributes
- Per-evaluation attributes take precedence over global ones
- This allows you to set common attributes globally and override them as needed
Using Features
The SDK provides multiple methods for evaluating features with different levels of detail.Basic Feature Checks
is_on() - Simple Boolean Check
Check if a feature is enabled (evaluates to a truthy value):
is_off() - Inverse Boolean Check
Check if a feature is disabled (evaluates to a falsy value):
Getting Feature Values
feature_result() - Get Detailed Feature Information
Get the full feature result with metadata:
Type-Safe Feature Values
Thevalue_as::<T>() method provides type-safe access to feature values:
Feature Result Properties
TheFeatureResult struct contains detailed information about the feature evaluation:
Handling Missing Features
Features that don’t exist returnNone as their value:
Feature Flags Usage - Best Practices
Tracking Callbacks
Tracking callbacks allow you to integrate GrowthBook with your analytics systems (Segment, Mixpanel, Amplitude, etc.) to track when users are exposed to experiments.Experiment Viewed Callback
This callback fires when a user is assigned a variation in an A/B test:When is the Callback Triggered?
Theon_experiment_viewed callback is called when:
- A feature evaluation runs an experiment
- The user is included in the experiment (passes targeting rules)
- The user is randomly assigned a variation (not forced)
- A feature uses a forced value (no experiment)
- The user is excluded from the experiment due to targeting
- The feature doesn’t exist
Feature Usage Callback
Track every feature evaluation, regardless of whether it’s part of an experiment:- Monitor which features are being evaluated
- Debug feature flag behavior
- Track adoption of new features
- Send metrics to monitoring systems (DataDog, New Relic)
Using Both Callbacks Together
You can use both callbacks for comprehensive tracking:Integration Examples
Segment Integration
Custom Analytics System
Context and Caching
Context
The SDK uses a context object internally to manage state. You typically don’t interact with it directly, but it’s useful to understand how it works:Caching
The SDK implements intelligent caching to minimize network requests:- Features are cached in memory after the first fetch
- Cache is automatically refreshed based on TTL
- Manual refresh with
client.refresh().awaitbypasses cache - Cache is shared across all evaluations
- TTL defaults to 60 seconds if not specified
Debugging and Logging
Enable Debug Output
The Rust SDK uses standard Rust logging. Enable it usingenv_logger or tracing:
Common Issues and Solutions
Issue: Features not loading
Issue: Wrong feature values
Issue: Auto-refresh not working
Testing and QA
Testing with Forced Values
Unit Testing Helpers
Integration Examples
This section provides real-world integration examples with popular Rust frameworks.Actix Web Integration
A complete example of integrating GrowthBook with Actix Web:Axum Integration
Modern async web framework integration:Rocket Integration
CLI Application Example
Using GrowthBook in a command-line application:Background Worker / Job Processor
Using GrowthBook in async background workers:Advanced Usage
Multiple SDK Instances
You can create multiple GrowthBook clients for different environments or projects:TypeScript / Rust Interop
If you’re building a hybrid application with TypeScript frontend and Rust backend, you can use the same SDK concepts across both: Rust Backend:Performance Considerations
Memory Usage
- Each client instance maintains an in-memory cache of features
- Auto-refresh spawns a background task
- Features are deserialized from JSON on each fetch
Network Performance
- Features are cached based on TTL
- Consider longer refresh intervals for stable features

