Back to Redis

redis-rs guide (Rust)

content/develop/clients/rust/_index.md

latest2.5 KB
Original Source

redis-rs is the Rust client for Redis. The sections below explain how to install redis-rs and connect your application to a Redis database.

{{< note >}}Although we provide basic documentation for redis-rs, it is a third-party client library and is not developed or supported directly by Redis. {{< /note >}}

redis-rs requires a running Redis server. See [here]({{< relref "/operate/oss_and_stack/install/" >}}) for Redis Open Source installation instructions.

Install

To use the synchronous API, add the redis crate as a dependency in your Cargo.toml file:

toml
[dependencies]
redis = "1.0.4"

If you want to use the asynchronous API, you should also enable either tokio or smol as your async platform:

toml
[dependencies]
# if you use tokio
tokio = { version = "1.32.0", features = ["full"] }
redis = { version = "1.0.4", features = ["tokio-comp"] }

# if you use smol
smol = "2.0.2"
redis = { version = "1.0.4", features = ["smol-comp"] }

Connect

Start by importing the Commands or AsyncCommands trait from the redis crate:

{{< clients-example set="landing" step="import" lang_filter="Rust-Sync,Rust-Async" description="Foundational: Import the Commands trait to access Redis command methods" difficulty="beginner" >}} {{< /clients-example >}}

The following example shows the simplest way to connect to a Redis server:

{{< clients-example set="landing" step="connect" lang_filter="Rust-Sync,Rust-Async" description="Foundational: Connect to a Redis server and establish a client connection" difficulty="beginner" >}} {{< /clients-example >}}

After connecting, you can test the connection by storing and retrieving a simple [string]({{< relref "/develop/data-types/strings" >}}):

{{< clients-example set="landing" step="set_get_string" lang_filter="Rust-Sync,Rust-Async" description="Foundational: Set and retrieve string values using SET and GET commands" difficulty="beginner" >}} {{< /clients-example >}}

You can also easily store and retrieve a [hash]({{< relref "/develop/data-types/hashes" >}}):

{{< clients-example set="landing" step="set_get_hash" lang_filter="Rust-Sync,Rust-Async" description="Foundational: Store and retrieve hash data structures using HSET and HGETALL" difficulty="beginner" >}} {{< /clients-example >}}

More information

See the redis-rs documentation and the GitHub repository for more information and examples.