Chapter 116 · Feature Flags Rust
Subchapter 116.3
references/rust.mdMarkdown4 KBView on GitHub
Install the posthog-rs crate by adding it to your Cargo.toml.
Cargo.toml
PostHog AI
[dependencies]
posthog-rs = "0.3.5"Next, set up the client with your PostHog project key.
Rust
PostHog AI
let client = posthog_rs::client(env!("<ph_project_token>"));Our Rust SDK supports both blocking and async clients. The async client is the default and is recommended for most use cases.
If you need to use a synchronous client instead – like we do in our CLI (opens in a new tab) –, you can opt into it by disabling the asynchronous feature on your Cargo.toml file.
toml
PostHog AI
[dependencies]
posthog-rs = { version = "0.3.5", default-features = false }In blocking mode, calls to capture and related methods will block until the PostHog event capture API returns – generally this is on the order of tens of milliseconds, but you may want to thread::spawn a background thread when you send an event.
Rust
PostHog AI
let is_enabled = client.is_feature_enabled(
"flag-key".to_string(),
"distinct_id_of_your_user".to_string(),
None, // groups
None, // person_properties
None, // group_properties
).await.unwrap();
if is_enabled {
// Do something differently for this user
}Rust
PostHog AI
use posthog_rs::FlagValue;
match client.get_feature_flag(
"flag-key".to_string(),
"distinct_id_of_your_user".to_string(),
None, None, None
).await.unwrap() {
Some(FlagValue::String(variant)) => {
if variant == "variant-key" {
// Do something for this variant
}
}
Some(FlagValue::Boolean(enabled)) => {
// Handle boolean flag
}
None => {
// Flag not found or disabled
}
}Rust
PostHog AI
let (flags, payloads) = client.get_feature_flags(
"distinct_id_of_your_user".to_string(),
None, None, None
).await.unwrap();
for (key, value) in flags {
println!("Flag {}: {:?}", key, value);
}Rust
PostHog AI
let payload = client.get_feature_flag_payload(
"flag-key".to_string(),
"distinct_id_of_your_user".to_string()
).await.unwrap();
if let Some(data) = payload {
println!("Payload: {}", data);
}Rust
PostHog AI
use std::collections::HashMap;
use serde_json::json;
let mut person_props = HashMap::new();
person_props.insert("plan".to_string(), json!("enterprise"));
person_props.insert("country".to_string(), json!("US"));
let flag = client.get_feature_flag(
"premium-feature".to_string(),
"distinct_id_of_your_user".to_string(),
None,
Some(person_props),
None
).await.unwrap();For B2B applications with group-based flags:
Rust
PostHog AI
use std::collections::HashMap;
use serde_json::json;
let mut groups = HashMap::new();
groups.insert("company".to_string(), "company-123".to_string());
let mut group_props = HashMap::new();
let mut company_props = HashMap::new();
company_props.insert("size".to_string(), json!(500));
group_props.insert("company".to_string(), company_props);
let flag = client.get_feature_flag(
"b2b-feature".to_string(),
"distinct_id_of_your_user".to_string(),
Some(groups),
None,
Some(group_props)
).await.unwrap();Now that you’re evaluating flags, continue with the resources below to learn what else Feature Flags enables within the PostHog platform.
| Resource | Description |
|---|---|
| Creating a feature flag (opens in a new tab) | How to create a feature flag in PostHog |
| Adding feature flag code (opens in a new tab) | How to check flags in your code for all platforms |
| Framework-specific guides (opens in a new tab) | Setup guides for React Native, Next.js, Flutter, and other frameworks |
| How to do a phased rollout (opens in a new tab) | Gradually roll out features to minimize risk |
| More tutorials (opens in a new tab) | Other real-world examples and use cases |
Ask a question
HelpfulCould be better