Rust SDK reference
Read time: 3 minutes
Last edited: Jul 22, 2024
Version 2 of the Rust SDK makes the rustls
dependency optional. There are no changes to the SDK API.
Version 1 of the Rust SDK replaces users with contexts. 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."
If you have been using the beta version, you can learn more about implementing version 1 by reading 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 |
Get started
After you complete the Get started process, follow these instructions to start using the LaunchDarkly SDK in your Rust application.
Install the SDK
First, install the LaunchDarkly SDK as a dependency in your application.
cargo add launchdarkly-server-sdk
Initialize the client
Next, import the LaunchDarkly client in your application code:
use launchdarkly_server_sdk::{Client, ConfigBuilder, ContextBuilder, ServiceEndpointsBuilder};
The Rust SDK uses an SDK key. Keys are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.
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-123abc).build();let client = Client::build(config).unwrap();client.start_with_default_executor();if !client.initialized_async().await {panic!("Client failed to successfully initialize");}}
To learn more about the specific configuration options available in this SDK, read Config
.
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.
Evaluate a context
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("context-key-123abc").build();let show_feature = client.bool_variation(&context, "flag-key-123abc", false);if show_feature {# application code to show the feature} else {# the code to run if the feature is off}
Shut down the client
Shut down the client when your application terminates. To learn more, read Shutting down.
Supported features
This SDK supports the following features:
- Anonymous contexts and users
- Configuration, including
- Context configuration
- Evaluating flags
- Flag evaluation reasons
- Flushing events
- Getting all flags
- Identifying and changing contexts
- Logging configuration
- Migrations
- Offline mode
- Private attributes
- Relay Proxy configuration
- Secure mode
- Sending custom events
- Service endpoint configuration
- Shutting down