> ## Documentation Index
> Fetch the complete documentation index at: https://launchdarkly.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Rust SDK reference

<View title="Developer" />

<View title="Federal docs" />

<View title="EU docs" />

<Note>
  **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](/docs/sdk/server-side/rust/migration-2-to-3).

  **Version 2 of the Rust SDK makes the `rustls` dependency optional**. There are no changes to the SDK API.
</Note>

This topic documents how to get started with the server-side Rust SDK, and links to reference information on all of the supported features.

<Note>
  **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:

  <table>
    <thead>
      <tr>
        <th>Resource</th>
        <th>Location</th>
      </tr>
    </thead>

    <tbody>
      <tr>
        <td>SDK API documentation</td>
        <td>[SDK API docs](https://docs.rs/launchdarkly-server-sdk)</td>
      </tr>

      <tr>
        <td>Supported SDK Versions</td>
        <td>[Rust server SDK](/docs/sdk/concepts/supported-versions#rust-server-sdk)</td>
      </tr>

      <tr>
        <td>GitHub repository</td>
        <td>[rust-server-sdk](https://github.com/launchdarkly/rust-server-sdk)</td>
      </tr>

      <tr>
        <td>Sample application</td>
        <td>[Rust](https://github.com/launchdarkly/hello-rust)</td>
      </tr>

      <tr>
        <td>Published module</td>
        <td>[crates.io](https://crates.io/crates/launchdarkly-server-sdk)</td>
      </tr>
    </tbody>
  </table>
</Note>

## Get started

After you complete the [Get started process](/docs/home/getting-started), 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.

<CodeGroup>
  ```bash title="Shell" lines wrap theme={null}
  cargo add launchdarkly-server-sdk
  ```
</CodeGroup>

### Initialize the client

Next, import the LaunchDarkly client in your application code:

<CodeGroup>
  ```rust title="Rust" lines wrap theme={null}
  use launchdarkly_server_sdk::{Client, ConfigBuilder, ContextBuilder, ServiceEndpointsBuilder};
  ```
</CodeGroup>

<Note>
  **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](/docs/sdk/concepts/client-side-server-side#keys-and-credentials).
</Note>

After you install and import the SDK, create a single, shared instance of the LaunchDarkly client. Specify your SDK key here because your application is authorized to connect to LaunchDarkly, your application, and your environment.

After 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:

<CodeGroup>
  ```rust title="Rust, v3.0+" expandable lines wrap theme={null}
  use std::time::Duration;

  #[tokio::main]
  async fn main() {
      let config = ConfigBuilder::new(&YOUR_SDK_KEY).build().unwrap();
      let client = Client::build(config).unwrap();

      client.start_with_default_executor();

      let initialized = client
          .wait_for_initialization(Duration::from_secs(10))
          .await
          .unwrap_or(false);

      if !initialized {
          panic!("Client failed to successfully initialize");
      }
  }
  ```
</CodeGroup>

To learn more about the specific configuration options available in this SDK, read [`Config`](https://docs.rs/launchdarkly-server-sdk/latest/launchdarkly_server_sdk/struct.Config.html).

<Warning>
  **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.
</Warning>

### Evaluate a context

You can use `client` to check which variation a particular context will receive for a given feature flag.

Here's how:

<CodeGroup>
  ```rust title="Rust" lines wrap theme={null}
  let context = ContextBuilder::new("example-context-key").build()?;
  let show_feature = client.bool_variation(&context, "example-flag-key", false);

  if show_feature {
    // application code to show the feature
  } else {
    // the code to run if the feature is off
  }
  ```
</CodeGroup>

## Shut down the client

Shut down the client when your application terminates. To learn more, read [Shutting down](/docs/sdk/features/shutdown#rust).

## Supported features

This SDK supports the following features:

* [SDK configuration](/docs/sdk/features/config#rust), including
  * [Application metadata configuration](/docs/sdk/features/app-config#rust)
* [Anonymous contexts and users](/docs/sdk/features/anonymous#rust)
* [Context configuration](/docs/sdk/features/context-config#rust)
* [Data saving mode](/docs/sdk/features/data-saving-mode#rust)
* [Flag variation evaluation](/docs/sdk/features/evaluating#rust)
* [Flag variation evaluation details](/docs/sdk/features/evaluation-reasons#rust)
* [Flushing events](/docs/sdk/features/flush#rust)
* [Getting all flags](/docs/sdk/features/all-flags#rust)
* [Identifying and changing contexts](/docs/sdk/features/identify#rust)
* [Logging configuration](/docs/sdk/features/logging#rust)
* [Migrations](/docs/sdk/features/migrations#rust)
* [Offline mode](/docs/sdk/features/offline-mode#rust)
* [Private attributes](/docs/sdk/features/private-attributes#rust)
* [Relay Proxy configuration](/docs/sdk/features/relay-proxy-configuration)
  * [Using proxy mode](/docs/sdk/features/relay-proxy-configuration/proxy-mode#rust)
  * [Using daemon mode](/docs/sdk/features/relay-proxy-configuration/daemon-mode#rust)
* [Secure mode](/docs/sdk/features/secure-mode#rust)
* [Tracking custom events](/docs/sdk/features/events#rust)
* [Service endpoint configuration](/docs/sdk/features/service-endpoint-configuration#rust)
* [Shutting down](/docs/sdk/features/shutdown#rust)
* [Test data sources](/docs/sdk/features/test-data-sources#rust)
* [Web proxy configuration](/docs/sdk/features/web-proxy#rust)
