|
| 1 | +--- |
| 2 | +categories: |
| 3 | +- docs |
| 4 | +- develop |
| 5 | +- stack |
| 6 | +- oss |
| 7 | +- rs |
| 8 | +- rc |
| 9 | +- oss |
| 10 | +- kubernetes |
| 11 | +- clients |
| 12 | +description: Connect your Rust application to a Redis database |
| 13 | +linkTitle: redis-rs (Rust) |
| 14 | +title: redis-rs guide (Rust) |
| 15 | +weight: 9 |
| 16 | +--- |
| 17 | + |
| 18 | +[`redis-rs`](https://github.com/redis-rs/redis-rs) is the [Rust](https://www.rust-lang.org/) client for Redis. |
| 19 | +The sections below explain how to install `redis-rs` and connect your application to a Redis database. |
| 20 | + |
| 21 | +{{< note >}}Although we provide basic documentation for `redis-rs`, it is a third-party |
| 22 | +client library and is not developed or supported directly by Redis. |
| 23 | +{{< /note >}} |
| 24 | + |
| 25 | +`redis-rs` requires a running Redis server. See [here]({{< relref "/operate/oss_and_stack/install/" >}}) for Redis Open Source installation instructions. |
| 26 | + |
| 27 | +## Install |
| 28 | + |
| 29 | +To use the synchronous API, add the `redis` crate as a dependency in your |
| 30 | +`Cargo.toml` file: |
| 31 | + |
| 32 | +```toml |
| 33 | +[dependencies] |
| 34 | +redis = "0.32.5" |
| 35 | +``` |
| 36 | + |
| 37 | +If you want to use the asynchronous API, you should also enable either |
| 38 | +[`tokio`](https://tokio.rs/) or [`smol`](https://crates.io/crates/smol) |
| 39 | +as your async platform: |
| 40 | + |
| 41 | +```toml |
| 42 | +[dependencies] |
| 43 | +# if you use tokio |
| 44 | +tokio = { version = "1.32.0", features = ["full"] } |
| 45 | +redis = { version = "0.32.5", features = ["tokio-comp"] } |
| 46 | + |
| 47 | +# if you use smol |
| 48 | +smol = "2.0.2" |
| 49 | +redis = { version = "0.32.5", features = ["smol-comp"] } |
| 50 | +``` |
| 51 | + |
| 52 | +## Connect |
| 53 | + |
| 54 | +Start by importing the `Commands` or `AsyncCommands` trait from the `redis` crate: |
| 55 | + |
| 56 | +{{< clients-example set="landing" step="import" lang_filter="Rust-Sync,Rust-Async" >}} |
| 57 | +{{< /clients-example >}} |
| 58 | + |
| 59 | +The following example shows the simplest way to connect to a Redis server: |
| 60 | + |
| 61 | +{{< clients-example set="landing" step="connect" lang_filter="Rust-Sync,Rust-Async" >}} |
| 62 | +{{< /clients-example >}} |
| 63 | + |
| 64 | +After connecting, you can test the connection by storing and retrieving |
| 65 | +a simple [string]({{< relref "/develop/data-types/strings" >}}): |
| 66 | + |
| 67 | +{{< clients-example set="landing" step="set_get_string" lang_filter="Rust-Sync,Rust-Async" >}} |
| 68 | +{{< /clients-example >}} |
| 69 | + |
| 70 | +You can also easily store and retrieve a [hash]({{< relref "/develop/data-types/hashes" >}}): |
| 71 | + |
| 72 | +{{< clients-example set="landing" step="set_get_hash" lang_filter="Rust-Sync,Rust-Async" >}} |
| 73 | +{{< /clients-example >}} |
| 74 | + |
| 75 | +## More information |
| 76 | + |
| 77 | +See the [`redis-rs`](https://docs.rs/redis/latest/redis/) documentation |
| 78 | +and the [GitHub repository](https://github.com/redis-rs/redis-rs) for more |
| 79 | +information and examples. |
0 commit comments