For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
Sign inTry it free
DocsGuidesSDKsIntegrationsAPI docsTutorialsFlagship blog
DocsGuidesSDKsIntegrationsAPI docsTutorialsFlagship blog
  • SDKs
    • SDK concepts
    • SDK features
    • Client-side SDKs
    • Server-side SDKs
      • .NET SDK reference
      • Apex SDK reference
      • C++ SDK reference
      • Erlang SDK reference
      • Go SDK reference
      • Haskell SDK reference
      • Java SDK reference
      • Lua SDK reference
      • Node.js SDK reference
      • PHP SDK reference
      • Python SDK reference
      • Ruby SDK reference
      • Rust SDK reference
        • Rust SDK 2.x to 3.0 migration guide
        • Rust SDK v1 implementation guide
    • AI SDKs
    • Edge SDKs
    • OpenFeature providers
    • Observability SDKs
    • Relay Proxy
Sign inTry it free
LogoLogo
On this page
  • Overview
  • Get started
  • Install the SDK
  • Initialize the client
  • Evaluate a context
  • Shut down the client
  • Supported features
SDKsServer-side SDKs

Rust SDK reference

Was this page helpful?
Previous

Rust SDK 2.x to 3.0 migration guide

Next
Built with
Recent major versions

Version 3 of the Rust SDK includes breaking changes. The most significant changes involve the HTTP transport layer, TLS/crypto feature flags, and some API refinements. To learn how to upgrade, read the Rust SDK 2.x to 3.0 migration guide.

Version 2 of the Rust SDK makes the rustls dependency optional. There are no changes to the SDK API.

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.

SDK quick links

LaunchDarkly’s SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and a sample application:

ResourceLocation
SDK API documentationSDK API docs
Supported SDK VersionsRust server SDK
GitHub repositoryrust-server-sdk
Sample applicationRust
Published modulecrates.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.

Shell
$cargo add launchdarkly-server-sdk

Initialize the client

Next, import the LaunchDarkly client in your application code:

Rust
1use launchdarkly_server_sdk::{Client, ConfigBuilder, ContextBuilder, ServiceEndpointsBuilder};
The Rust SDK uses an SDK key

The Rust SDK uses an SDK key. Keys are specific to each project and environment. They are available on the SDK keys page under Settings. 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:

Rust, v3.0+
1use std::time::Duration;
2
3#[tokio::main]
4async fn main() {
5 let config = ConfigBuilder::new(&YOUR_SDK_KEY).build();
6 let client = Client::build(config).unwrap();
7
8 client.start_with_default_executor();
9
10 let initialized = client
11 .wait_for_initialization(Duration::from_secs(10))
12 .await
13 .unwrap_or(false);
14
15 if !initialized {
16 panic!("Client failed to successfully initialize");
17 }
18}

To learn more about the specific configuration options available in this SDK, read Config.

client must be a singleton

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:

Rust
1let context = ContextBuilder::new("example-context-key").build();
2let show_feature = client.bool_variation(&context, "example-flag-key", false);
3
4if show_feature {
5 # application code to show the feature
6} else {
7 # the code to run if the feature is off
8}

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
    • Application metadata configuration
  • 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
    • Using proxy mode
    • Using daemon mode
  • Secure mode
  • Sending custom events
  • Service endpoint configuration
  • Shutting down
  • Test data sources
  • Web proxy configuration