redis-rs guide (Rust)

Connect your Rust application to a Redis database

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.

redis-rs requires a running Redis server. See here for Redis Open Source installation instructions.

Install

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

[dependencies]
redis = "0.32.5"

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

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

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

Connect

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

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

After connecting, you can test the connection by storing and retrieving a simple string:

You can also easily store and retrieve a hash:

More information

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

RATE THIS PAGE
Back to top ↑