# Rust experiments installation - Docs

1.  1

    ## Install PostHog Rust SDK

    Required

    Install the `posthog-rs` crate by adding it to your `Cargo.toml`.

    Cargo.toml

    PostHog AI

    ```toml
    [dependencies]
    posthog-rs = "0.3.5"
    ```

    Next, set up the client with your PostHog project key.

    Rust

    PostHog AI

    ```rust
    let client = posthog_rs::client(env!("<ph_project_token>"));
    ```

    ### Blocking client

    Our Rust SDK supports both blocking and async clients. The async client is the default and is recommended for most use cases.

    If you need to use a synchronous client instead – like we do in our [CLI](https://github.com/PostHog/posthog/tree/master/cli) –, you can opt into it by disabling the asynchronous feature on your `Cargo.toml` file.

    toml

    PostHog AI

    ```toml
    [dependencies]
    posthog-rs = { version = "0.3.5", default-features = false }
    ```

    In blocking mode, calls to `capture` and related methods will block until the PostHog event capture API returns – generally this is on the order of tens of milliseconds, but you may want to `thread::spawn` a background thread when you send an event.

2.  2

    ## Capture conversion event

    Required

    Once PostHog is initialized, you should be able to capture events. For your experiment to be meaningful, we need to capture an event that we want to measure, such as a conversion event.

    For this tutorial, let's report a conversion event when a user clicks a CTA. In server SDKs like Rust, this can be called when a specific route is visited or reported from your client side app.

    Rust

    PostHog AI

    ```rust
    use posthog_rs::Event;
    let event = Event::new("cta clicked", "user_distinct_id");
    client.capture(event).await.unwrap();
    ```

3.  ## Validate PostHog events

    Checkpoint

    *Confirm events are being sent to PostHog*

    Before proceeding, let's make sure events are being captured and sent to PostHog. You should see `cta clicked` events appear in the **Activity** feed.

    [Check for events in PostHog](https://app.posthog.com/activity/explore)

4.  3

    ## Create an experiment

    Required

    Go to the [**Experiments** tab](https://app.posthog.com/experiments) in the PostHog app and click on the **New experiment** button in the top right.

    ![Create experiment](https://res.cloudinary.com/dmukukwp6/image/upload/create_experiment_ae83e877d6.png)![Create experiment](https://res.cloudinary.com/dmukukwp6/image/upload/create_experiment_dark_5c6a4acbd0.png)

    For this tutorial, let's create a new experiment using simplified test values:

    -   **Name**: "Test experiment"
    -   **Description**: "This is a test experiment"
    -   **Feature flag key**: "test-experiment-ff-key"
    -   **Experiment type**: "Feature flag"
    -   **Variants**: "control" and "test"
    -   **Participant type**: "Users"

    Then click **Save as draft**.

5.  4

    ## Add primary metric and launch

    Required

    Scroll down to the **Primary metrics** section and click **\+ Add primary metric**.

    Choose **Single-use** and select **Type** > **Mean**.

    Then search for the event `cta clicked` under **Metric** and click **Save**.

    ![Add primary metric](https://res.cloudinary.com/dmukukwp6/image/upload/primary_metric_b8d831b522.png)![Add primary metric](https://res.cloudinary.com/dmukukwp6/image/upload/primary_metric_dark_a24636046b.png)

    By default, experiments are exposed to 100% of users. You can customize [release conditions](/docs/experiments/creating-an-experiment.md#release-conditions) to expose the experiment to a subset of users.

    For this tutorial, we'll ship the experiment to all users and click **Launch** in the top right.

6.  5

    ## Call feature flag

    Required

    Use the PostHog SDK to call the experiment flag and update how your page renders based on the assigned variant.

    Rust

    PostHog AI

    ```rust
    use posthog_rs::FlagValue;
    match client.get_feature_flag(
        "your-experiment-key".to_string(),
        "user_distinct_id".to_string(),
        None, None, None
    ).await.unwrap() {
        Some(FlagValue::String(variant)) if variant == "test" => {
            // Show test variant UI
        }
        _ => {
            // Show control variant UI
        }
    }
    ```

    Now when a user triggers a `cta clicked` event, PostHog automatically assigns the user to a variant and records an experiment exposure.

    By default, users are split equally between variants. If you want to assign specific users to a specific variant, see more about [distribution and release conditions](/docs/experiments/creating-an-experiment.md#distribution-and-release-conditions).

7.  ## Validate feature flag calls

    Checkpoint

    Make sure exposures and feature flag calls are being sent to PostHog. You should see `$feature_flag_called` events appear in the **Activity** feed.

    [Check for events in PostHog](https://app.posthog.com/activity/explore)

8.  6

    ## Evaluate experiment results

    Recommended

    As you capture more `cta clicked` events, more exposures will populate the primary metrics in your experiment.

    ![Evaluate experiment metrics](https://res.cloudinary.com/dmukukwp6/image/upload/experiment_metrics_ecea40f310.png)![Evaluate experiment metrics](https://res.cloudinary.com/dmukukwp6/image/upload/experiment_metrics_dark_6fed5063b9.png)

    With enough data, you can analyze the experiment and its variants by:

    -   Conversion rates
    -   Statistical significance
    -   Credible intervals
    -   Chance to win %
    -   Minimum detectable effect
    -   And more

### Community questions

Ask a question

### Was this page useful?

HelpfulCould be better