Quickstart: Rust

This tutorial will help you start sending data from your Rust servers to Segment and any of our destinations, using our Rust library. As soon as you’re set up you’ll be able to turn on any new destinations with the flip of a switch!

If you want to dive deeper at any point, check out the Rust library reference.

Step 1: Create a Source in the Segment app

Before you begin, you need a Workspace (which is a container that holds all of the sources and destinations which are billed together for an organization). If you already created one, great! If not, you can sign up for a free Segment account and create one.

Next, create a Rust source from your Workspace:

  1. Click Add Source.
  2. From the source catalog page, click Rust.
  3. Click Add Source again from the informational panel that appears to the right.
  4. Give the source a display name, and enter the URL the source will collect data from.

When you create a Source in the Segment web app, it tells the Segment servers that you’ll be sending data from a specific source type. When you create (or change!) a Source in the Segment app, Segment generates a new Write Key for that source. You use the write key in your code to tell the Segment servers where the data is coming from, so Segment can route it to your destinations and other tools.

Step 2: Install the Package

Installing our Rust library is easy, just run the following from your code’s root directory:

cargo add analytics

which will add the following to you Cargo.toml file:

analytics = "0.2"

Then use it with your source’s Write Key. Of course, you’ll want to replace YOUR_WRITE_KEY with your actual Write Key which you can find in Segment under your source settings.

use analytics::http::HttpClient;
use analytics::client::Client;
use analytics::message::{BatchMessage, Track, User};
use analytics::batcher::Batcher;
use serde_json::json;

fn main() {
    let write_key = "YOUR_WRITE_KEY";
    let client = HttpClient::default();
    let mut batcher = Batcher::new(None);

    // Pretend this is reading off of a queue, a file, or some other data
    // source.
    for i in 0..100 {
        let msg = BatchMessage::Track(Track {
            user: User::UserId { user_id: format!("user-{}", i) },
            event: "Example Event".to_owned(),
            properties: json!({
                "foo": format!("bar-{}", i),

        // An error here indicates a message is too large. In real life, you
        // would probably want to put this message in a deadletter queue or some
        // equivalent.
        if let Some(msg) = batcher.push(msg).unwrap() {
            client.send(write_key, &batcher.into_message()).unwrap();

            batcher = Batcher::new(None);
            batcher.push(msg).unwrap(); // Same error condition as above.

That will create a client that you can use to send data to Segment for your source. Once you’ve got that, you’re ready to…

Step 3: Identify Users

The identify method is how you tell Segment who the current user is. It includes a unique User ID and any optional traits you know about them. You can read more about it in the identify reference.

Here’s what a basic call to identify might look like:

  user: User::UserId { user_id: format!("user-{}", i) },
  traits: json!({
    "name":"Michael Bolton",

That’s identifying Michael by his unique User ID (the one you know him by in your database) and labeling him with name and email traits.

When you’re using our Rust library, you don’t need to identify a user on every request they make to your servers. Instead, we recommend calling identify a single time when the user’s account is first created, and only identifying again later when their traits are change.

Once you’ve added an identify call, you can move on to…

Step 4: Track Actions

The track method is how you tell Segment about which actions your users are performing. Every action triggers what we call an “event”, which can also have associated properties. You can read more about track in the track reference.

Here’s what a call to track might look like when a user signs up:

Track {
    user: User::UserId { user_id: "f4ca124298".to_owned() },
    event: "Signed Up".to_owned(),
    properties: json!({
        "plan": "Enterprise",

That’s just telling us that your user just triggered the Signed Up event and chose your hypothetical 'Enterprise' plan. Properties can be anything you want to record, for example:

Track {
    user: User::UserId { user_id: "f4ca124298".to_owned() },
    event: "Article Bookmarked".to_owned(),
    properties: json!({
        "title": "Snow Fall",
        "subtitle": "The Avalanche at Tunnel Creek",
        "author": "John Branch"

You’ll want to track events that are indicators of success for your site, like Signed Up, Item Purchased or Article Bookmarked.

To get started, we recommend tracking just a few important events. You can always add more later!

Once you’ve added a few track calls, you’re done! You successfully installed analytics tracking on your servers. Now you’re ready to turn on any destination you fancy from our interface, margarita in hand.

What’s Next?

We just walked through the quickest way to get started with Segment using go. You might also want to check out our full Go library reference to see what else is possible, or read about the Tracking API methods to get a sense for the bigger picture.

You might also want to consider installing Analytics.js so that you can use destinations that require being loaded in the browser, like live chat tools or user feedback systems.

This page was last modified: 06 Aug 2020

Get started with Segment

Segment is the easiest way to integrate your websites & mobile apps data to over 300 analytics and growth tools.
Create free account