Skip to main content
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.

Requirements

  • Rust 1.70.0 or higher (as specified in rust-toolchain)
  • Async runtime: Tokio (recommended) or any async-std compatible runtime

Installation

Add this to your Cargo.toml:
Or install via cargo:

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

When auto_refresh is enabled:
  1. Features are fetched immediately during build()
  2. A background task spawns that periodically fetches updates
  3. The cache is updated automatically without blocking your application
  4. The refresh task runs until the client is dropped
Benefits:
  • 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:

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

  1. Enable encryption in your GrowthBook SDK Connection settings
  2. Copy the decryption key shown in the GrowthBook dashboard
  3. 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

Recommendations:
  • 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:
  1. Feature targeting - Show different values to different user segments
  2. 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:
  1. They are merged with global attributes
  2. Per-evaluation attributes take precedence over global ones
  3. 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

The value_as::<T>() method provides type-safe access to feature values:

Feature Result Properties

The FeatureResult struct contains detailed information about the feature evaluation:

Handling Missing Features

Features that don’t exist return None 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?

The on_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)
It is NOT called when:
  • 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:
Use Cases for Feature Usage Tracking:
  • 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:
How Caching Works:
  • Features are cached in memory after the first fetch
  • Cache is automatically refreshed based on TTL
  • Manual refresh with client.refresh().await bypasses cache
  • Cache is shared across all evaluations
  • TTL defaults to 60 seconds if not specified
Cache Behavior:

Debugging and Logging

Enable Debug Output

The Rust SDK uses standard Rust logging. Enable it using env_logger or tracing:
Set the log level via environment variable:

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:
TypeScript Frontend:

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
Optimization Tips:

Network Performance

  • Features are cached based on TTL
  • Consider longer refresh intervals for stable features

Troubleshooting

Common Error Messages

”Failed to fetch features"

"Decryption failed"

"Feature not found”

Enable Verbose Logging

Health Check Endpoint

Migration from Other SDKs

From Node.js/JavaScript SDK

JavaScript:
Rust:

From Python SDK

Python:
Rust:

Further Reading

Supported Features