Rust SDK reference
Read time: 1 minute
Last edited: Mar 16, 2023
A context is a generalized way of referring to the people, services, machines, or other resources that encounter feature flags in your product. Contexts replace another data object in LaunchDarkly: "users."
Code samples on this page are from version 1 of the Rust SDK. To learn more about implementing version 1 if you have been using the beta version, read Rust SDK v1 implementation guide and Best practices for upgrading users to contexts.
Overview
This topic documents how to get started with the server-side Rust SDK, and links to reference information on all of the supported features.
LaunchDarkly's SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and a sample application:
Resource | Location |
---|---|
SDK API documentation | SDK API docs |
GitHub repository | rust-server-sdk |
Sample application | Rust |
Published module | crates.io |
Getting started
After you complete the Getting Started process, follow these instructions to start using the LaunchDarkly SDK in your Rust application.
First, install the LaunchDarkly SDK as a dependency in your application.
cargo add launchdarkly-server-sdk
Next, import the LaunchDarkly client in your application code:
use launchdarkly_server_sdk::{Client, ConfigBuilder, ContextBuilder, ServiceEndpointsBuilder};
After you install and import the SDK, create a single, shared instance of the LaunchDarkly client. Specify your SDK key here so that your application is authorized to connect to LaunchDarkly, your application, and your environment.
Once you have created the client, start the client process and wait for the client to initialize. This SDK depends on the tokio crate to provide a default runtime and as such it is a required dependency.
Only create one instance of client
.
Here's how:
#[tokio::main]async fn main () {let config = ConfigBuilder::new(&sdk_key).build();let client = Client::build(config).unwrap();client.start_with_default_executor();if !client.initialized_async().await {panic!("Client failed to successfully initialize");}}
It's important to make client
a singleton for each LaunchDarkly project. The client instance maintains internal state that allows LaunchDarkly to serve feature flags without making any remote requests. Do not instantiate a new client with every request.
If you have multiple LaunchDarkly projects, you can create one LDClient
for each. In this situation, the clients operate independently. For example, they do not share a single connection to LaunchDarkly.
You can use client
to check which variation a particular context will receive for a given feature flag.
Here's how:
let context = ContextBuilder::new("specific.context.key").build();let show_feature = client.bool_variation(&context, "your.flag.key", false);if show_feature {# application code to show the feature} else {# the code to run if the feature is off}
Supported features
This SDK supports the following features:
- Anonymous contexts and users
- Configuration
- Evaluating flags
- Flag evaluation reasons
- Flushing events
- Getting all flags
- Identifying and changing contexts
- Logging configuration
- Offline mode
- Private attributes
- Secure mode
- Sending custom events
- Service endpoint configuration
- Relay Proxy configuration
- Shutting down
- User and context configuration