Skip to main content

Getting Started with Momento Storage in Rust

If you need to get going quickly with Rust and Momento Storage, this page contains the basic API calls you'll need. Check the Rust SDK examples for complete, working examples including build configuration files.

Install the Momento SDK

The Momento SDK is available on crates.io: momento.

tip

Visit crates.io to find the latest available version of the SDK.

Install the client library in an existing Rust project:

cargo add momento

Set up your API key

You'll need a Momento API key to authenticate with Momento. You can get one from the Momento Web Console. Once you have your API key, store it in an environment variable so that the Momento client can consume it:

export MOMENTO_API_KEY=<your Momento API key here>

Import libraries and connect to return a StorageClient object

This code sets up the main function, the necessary imports, and the StorageClient that will be used to interact with your store.

use momento::{
storage, CredentialProvider, MomentoResult, PreviewStorageClient,
};

#[tokio::main]
async fn main() -> MomentoResult<()> {
let storage_client = PreviewStorageClient::builder()
.configuration(storage::configurations::Laptop::latest())
.credential_provider(
CredentialProvider::from_env_var("MOMENTO_API_KEY".to_string())
.expect("API key should be valid"),
)
.build()?;

// ...

Ok(())
}

Create a new store in Momento Storage

Use this function to create a new store in your account.

  let response = storage_client.create_store(store_name).await?;
match response {
momento::storage::CreateStoreResponse::Created => println!("Store {} created", store_name),
momento::storage::CreateStoreResponse::AlreadyExists => println!("Store {} already exists", store_name),
}

List the names of existing stores in your account

A simple list of the names of stores for the account.

  let response = storage_client.list_stores().await?;
println!("Stores: {:#?}", response.stores);

Write an item to a store

A simple example of doing a put operation.

  storage_client.put(store_name, "key", "value").await?;
println!("Put key and value in store {}", store_name);

Read an item from a store

This is an example of a simple read operation to get an item from a store.

  let response = storage_client.get(store_name, "key").await?;
match response {
momento::storage::GetResponse::NotFound => println!("Key not found in {}", store_name),
momento::storage::GetResponse::Found { value } => {
// A Found response indicates the value was found in the store.

// Use `match` to get the value if you don't know the type beforehand:
match value.clone() {
momento::storage::StorageValue::String(value) => println!("Got string value {}", value),
momento::storage::StorageValue::Bytes(value) => println!("Got bytes value {:?}", value),
momento::storage::StorageValue::Integer(value) => println!("Got integer value {}", value),
momento::storage::StorageValue::Double(value) => println!("Got double value {}", value),
}

// If you know the type you're expecting, you can `try_into()` it directly:
let found_value: String = value.try_into()?;
println!("Got value {}", found_value);
}
}

Running the code

You can find complete, working examples in the Rust SDK github repo examples directory.

info

Beyond these basic API calls check out the API reference page for more information on the full assortment of Momento API calls.