{
  "schema_version": 2,
  "id": "develop/clients/rust",
  "title": "redis-rs guide (Rust)",
  "url": "https://redis.io/docs/latest/develop/clients/rust/",
  "summary": "Connect your Rust application to a Redis database",
  "aliases": [
    "/connect/clients/rust/",
    "/clients/rust/",
    "/develop/clients/redis-rs"
  ],
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-10-12T14:48:04+01:00",
  "children": [
    {
      "id": "develop/clients/rust/connect",
      "summary": "Connect your Rust application to a Redis database",
      "title": "Connect to the server",
      "url": "https://redis.io/docs/latest/develop/clients/rust/connect/"
    },
    {
      "id": "develop/clients/rust/amr",
      "summary": "Learn how to authenticate to an Azure Managed Redis (AMR) database",
      "title": "Connect to Azure Managed Redis",
      "url": "https://redis.io/docs/latest/develop/clients/rust/amr/"
    },
    {
      "id": "develop/clients/rust/json",
      "summary": "Learn how to store, read, and update JSON documents with redis-rs.",
      "title": "Work with JSON documents",
      "url": "https://redis.io/docs/latest/develop/clients/rust/json/"
    },
    {
      "id": "develop/clients/rust/prob",
      "summary": "Learn how to use Bloom filter set membership and HyperLogLog approximate cardinality with redis-rs.",
      "title": "Probabilistic data types",
      "url": "https://redis.io/docs/latest/develop/clients/rust/prob/"
    },
    {
      "id": "develop/clients/rust/error-handling",
      "summary": "Learn how to handle errors when using redis-rs",
      "title": "Error handling",
      "url": "https://redis.io/docs/latest/develop/clients/rust/error-handling/"
    },
    {
      "id": "develop/clients/rust/transpipe",
      "summary": "Learn how to use Redis pipelines and transactions",
      "title": "Pipelines and transactions",
      "url": "https://redis.io/docs/latest/develop/clients/rust/transpipe/"
    }
  ],
  "page_type": "content",
  "content_hash": "7f84b6fada4364c168227b26834c2e46e8e70f35112bbefa61122433b1b22a09",
  "sections": [
    {
      "id": "install",
      "title": "Install",
      "role": "setup",
      "text": "To use the synchronous API, add the `redis` crate as a dependency in your\n`Cargo.toml` file:\n\n[code example]\n\nIf you want to use the asynchronous API, you should also enable either\n[`tokio`](https://tokio.rs/) or [`smol`](https://crates.io/crates/smol)\nas your async platform:\n\n[code example]"
    },
    {
      "id": "connect",
      "title": "Connect",
      "role": "content",
      "text": "Start by importing the `Commands` or `AsyncCommands` trait from the `redis` crate:\n\nFoundational: Import the Commands trait to access Redis command methods\n\n**Difficulty:** Beginner\n\n**Available in:** Rust (Asynchronous), Rust (Synchronous)\n\n##### Rust (Asynchronous)\n\n[code example]\n\n##### Rust (Synchronous)\n\n[code example]\n\n\n\nThe following example shows the simplest way to connect to a Redis server:\n\nFoundational: Connect to a Redis server and establish a client connection\n\n**Difficulty:** Beginner\n\n**Available in:** Rust (Asynchronous), Rust (Synchronous)\n\n##### Rust (Asynchronous)\n\n[code example]\n\n##### Rust (Synchronous)\n\n[code example]\n\n\n\n> [!NOTE]\n> redis-rs supports [connection URLs](https://redis.io/docs/latest/develop/clients/connection-urls#redis-rs-rust) through `Client::open()`.\n> A URL can set the host, port, credentials, database, and whether to use TLS,\n> so you can use one instead of passing those options separately.\n\nAfter connecting, you can test the connection by  storing and retrieving\na simple [string](https://redis.io/docs/latest/develop/data-types/strings):\n\nFoundational: Set and retrieve string values using SET and GET commands\n\n**Difficulty:** Beginner\n\n**Available in:** Rust (Asynchronous), Rust (Synchronous)\n\n##### Rust (Asynchronous)\n\n[code example]\n\n##### Rust (Synchronous)\n\n[code example]\n\n\n\nYou can also easily store and retrieve a [hash](https://redis.io/docs/latest/develop/data-types/hashes):\n\nFoundational: Store and retrieve hash data structures using HSET and HGETALL\n\n**Difficulty:** Beginner\n\n**Available in:** Rust (Asynchronous), Rust (Synchronous)\n\n##### Rust (Asynchronous)\n\n[code example]\n\n##### Rust (Synchronous)\n\n[code example]\n\n\n\nTo connect to a cluster, through Redis Sentinel, or over TLS, see\n[Connect to the server](https://redis.io/docs/latest/develop/clients/rust/connect)."
    },
    {
      "id": "more-information",
      "title": "More information",
      "role": "content",
      "text": "See the [`redis-rs`](https://docs.rs/redis/latest/redis/) documentation\nand the [GitHub repository](https://github.com/redis-rs/redis-rs) for more\ninformation and examples."
    }
  ],
  "examples": [
    {
      "id": "install-ex0",
      "language": "toml",
      "code": "[dependencies]\nredis = \"1.0.4\"",
      "section_id": "install"
    },
    {
      "id": "install-ex1",
      "language": "toml",
      "code": "[dependencies]\n# if you use tokio\ntokio = { version = \"1.32.0\", features = [\"full\"] }\nredis = { version = \"1.0.4\", features = [\"tokio-comp\"] }\n\n# if you use smol\nsmol = \"2.0.2\"\nredis = { version = \"1.0.4\", features = [\"smol-comp\"] }",
      "section_id": "install"
    },
    {
      "id": "connect-ex0",
      "language": "rust",
      "code": "use redis::AsyncCommands;",
      "section_id": "connect"
    },
    {
      "id": "connect-ex1",
      "language": "rust",
      "code": "use redis::Commands;",
      "section_id": "connect"
    },
    {
      "id": "connect-ex2",
      "language": "rust",
      "code": "let mut r = match redis::Client::open(\"redis://127.0.0.1\") {\n            Ok(client) => {\n                match client.get_multiplexed_async_connection().await {\n                    Ok(conn) => conn,\n                    Err(e) => {\n                        println!(\"Failed to connect to Redis: {e}\");\n                        return;\n                    }\n                }\n            },\n            Err(e) => {\n                println!(\"Failed to create Redis client: {e}\");\n                return;\n            }\n        };",
      "section_id": "connect"
    },
    {
      "id": "connect-ex3",
      "language": "rust",
      "code": "let mut r = match redis::Client::open(\"redis://127.0.0.1\") {\n            Ok(client) => {\n                match client.get_connection() {\n                    Ok(conn) => conn,\n                    Err(e) => {\n                        println!(\"Failed to connect to Redis: {e}\");\n                        return;\n                    }\n                }\n            },\n            Err(e) => {\n                println!(\"Failed to create Redis client: {e}\");\n                return;\n            }\n        };",
      "section_id": "connect"
    },
    {
      "id": "connect-ex4",
      "language": "rust",
      "code": "if let Ok(res) = r.set(\"foo\", \"bar\").await {\n            let res: String = res;\n            println!(\"{res}\"); // >>> OK\n        } else {\n            println!(\"Error setting foo\");\n        }\n\n        match r.get(\"foo\").await {\n            Ok(res) => {\n                let res: String = res;\n                println!(\"{res}\"); // >>> bar\n            },\n            Err(e) => {\n                println!(\"Error getting foo: {e}\");\n                return;\n            }\n        };",
      "section_id": "connect"
    },
    {
      "id": "connect-ex5",
      "language": "rust",
      "code": "if let Ok(res) = r.set(\"foo\", \"bar\") {\n            let res: String = res;\n            println!(\"{res}\");    // >>> OK\n        } else {\n            println!(\"Error setting foo\");\n        }\n        \n        match r.get(\"foo\") {\n            Ok(res) => {\n                let res: String = res;\n                println!(\"{res}\");   // >>> bar\n            },\n            Err(e) => {\n                println!(\"Error getting foo: {e}\");\n                return;\n            }\n        };",
      "section_id": "connect"
    },
    {
      "id": "connect-ex6",
      "language": "rust",
      "code": "let hash_fields = [\n            (\"model\", \"Deimos\"),\n            (\"brand\", \"Ergonom\"),\n            (\"type\", \"Enduro bikes\"),\n            (\"price\", \"4972\"),\n        ];\n\n        if let Ok(res) = r.hset_multiple(\"bike:1\", &hash_fields).await {\n            let res: String = res;\n            println!(\"{res}\"); // >>> OK\n        } else {\n            println!(\"Error setting bike:1\");\n        }\n\n        match r.hget(\"bike:1\", \"model\").await {\n            Ok(res) => {\n                let res: String = res;\n                println!(\"{res}\"); // >>> Deimos\n            },\n            Err(e) => {\n                println!(\"Error getting bike:1 model: {e}\");\n                return;\n            }\n        }\n\n        match r.hget(\"bike:1\", \"price\").await {\n            Ok(res) => {\n                let res: String = res;\n                println!(\"{res}\"); // >>> 4972\n            },\n            Err(e) => {\n                println!(\"Error getting bike:1 price: {e}\");\n                return;\n            }\n        }\n\n        match r.hgetall(\"bike:1\").await {\n            Ok(res) => {\n                let res: Vec<(String, String)> = res;\n\n                for (key, value) in res {\n                    println!(\"{key}: {value}\");\n                }\n                // >>> model: Deimos\n                // >>> brand: Ergonom\n                // >>> type: Enduro bikes\n                // >>> price: 4972\n            },\n            Err(e) => {\n                println!(\"Error getting bike:1: {e}\");\n                return;\n            }",
      "section_id": "connect"
    },
    {
      "id": "connect-ex7",
      "language": "rust",
      "code": "let hash_fields = [\n            (\"model\", \"Deimos\"),\n            (\"brand\", \"Ergonom\"),\n            (\"type\", \"Enduro bikes\"),\n            (\"price\", \"4972\"),\n        ];\n    \n        if let Ok(res) = r.hset_multiple(\"bike:1\", &hash_fields) {\n            let res: String = res;\n            println!(\"{res}\"); // >>> OK\n        } else {\n            println!(\"Error setting bike:1\");\n        }\n    \n        match r.hget(\"bike:1\", \"model\") {\n            Ok(res) => {\n                let res: String = res;\n                println!(\"{res}\"); // >>> Deimos\n            },\n            Err(e) => {\n                println!(\"Error getting bike:1 model: {e}\");\n                return;\n            }   \n        }\n    \n        match r.hget(\"bike:1\", \"price\") {\n            Ok(res) => {\n                let res: String = res;\n                println!(\"{res}\"); // >>> 4972\n            },\n            Err(e) => {\n                println!(\"Error getting bike:1 price: {e}\");\n                return;\n            }   \n        }\n    \n        match r.hgetall(\"bike:1\") {\n            Ok(res) => {\n                let res: Vec<(String, String)> = res;\n                \n                for (key, value) in res {\n                    println!(\"{key}: {value}\");\n                }\n                // >>> model: Deimos\n                // >>> brand: Ergonom\n                // >>> type: Enduro bikes\n                // >>> price: 4972\n            },\n            Err(e) => {\n                println!(\"Error getting bike:1: {e}\");\n                return;\n            }   \n        }",
      "section_id": "connect"
    }
  ]
}
