Rust SDK reference
Read time: 1 minute
Last edited: May 13, 2022
Development work on the Rust SDK is ongoing. Elements of this SDK may change without notice. Do not use this SDK in production environments.
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 | hello-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 install cargo-editcargo add launchdarkly-server-sdk --allow-prerelease
Next, import the LaunchDarkly client in your application code:
use launchdarkly_server_sdk::{Client, ConfigBuilder, User};
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. 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.
You can use client
to check which variation a particular user will receive for a given feature flag.
Here's how:
let user = User::with_key("user@test.com").build();let show_feature = client.bool_variation(&user, "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: