VOOZH about

URL: https://redis.io/docs/latest/develop/clients/rust/

⇱ redis-rs guide (Rust) | Docs


{"categories":["docs","develop","stack","oss","rs","rc","oss","kubernetes","clients"],"description":"Connect your Rust application to a Redis database","duplicateOf":"head:data-ai-metadata","location":"body","title":"redis-rs guide (Rust)","tableOfContents":{"sections":[{"id":"install","title":"Install"},{"id":"connect","title":"Connect"},{"id":"more-information","title":"More information"}]},"codeExamples":[{"codetabsId":"landing-stepimport","description":"Foundational: Import the Commands trait to access Redis command methods","difficulty":"beginner","id":"import","languages":[{"clientId":"redis-rs","clientName":"redis-rs","id":"Rust-Sync","langId":"rust","panelId":"panel_Rust-Sync_landing-stepimport"},{"clientId":"redis-rs","clientName":"redis-rs","id":"Rust-Async","langId":"rust","panelId":"panel_Rust-Async_landing-stepimport"}]},{"codetabsId":"landing-stepconnect","description":"Foundational: Connect to a Redis server and establish a client connection","difficulty":"beginner","id":"connect","languages":[{"clientId":"redis-rs","clientName":"redis-rs","id":"Rust-Sync","langId":"rust","panelId":"panel_Rust-Sync_landing-stepconnect"},{"clientId":"redis-rs","clientName":"redis-rs","id":"Rust-Async","langId":"rust","panelId":"panel_Rust-Async_landing-stepconnect"}]},{"codetabsId":"landing-stepset_get_string","description":"Foundational: Set and retrieve string values using SET and GET commands","difficulty":"beginner","id":"set_get_string","languages":[{"clientId":"redis-rs","clientName":"redis-rs","id":"Rust-Sync","langId":"rust","panelId":"panel_Rust-Sync_landing-stepset_get_string"},{"clientId":"redis-rs","clientName":"redis-rs","id":"Rust-Async","langId":"rust","panelId":"panel_Rust-Async_landing-stepset_get_string"}]},{"codetabsId":"landing-stepset_get_hash","description":"Foundational: Store and retrieve hash data structures using HSET and HGETALL","difficulty":"beginner","id":"set_get_hash","languages":[{"clientId":"redis-rs","clientName":"redis-rs","id":"Rust-Sync","langId":"rust","panelId":"panel_Rust-Sync_landing-stepset_get_hash"},{"clientId":"redis-rs","clientName":"redis-rs","id":"Rust-Async","langId":"rust","panelId":"panel_Rust-Async_landing-stepset_get_hash"}]}]}

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 = "1.0.4"

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 = "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:

Foundational: Import the Commands trait to access Redis command methods
useredis::Commands;
useredis::AsyncCommands;

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

Foundational: Connect to a Redis server and establish a client connection
letmutr=matchredis::Client::open("redis://127.0.0.1"){Ok(client)=>{matchclient.get_connection(){Ok(conn)=>conn,Err(e)=>{println!("Failed to connect to Redis: {e}");return;}}},Err(e)=>{println!("Failed to create Redis client: {e}");return;}};
letmutr=matchredis::Client::open("redis://127.0.0.1"){Ok(client)=>{matchclient.get_multiplexed_async_connection().await{Ok(conn)=>conn,Err(e)=>{println!("Failed to connect to Redis: {e}");return;}}},Err(e)=>{println!("Failed to create Redis client: {e}");return;}};

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

Foundational: Set and retrieve string values using SET and GET commands
ifletOk(res)=r.set("foo","bar"){letres: String =res;println!("{res}");// >>> OK
}else{println!("Error setting foo");}matchr.get("foo"){Ok(res)=>{letres: String =res;println!("{res}");// >>> bar
},Err(e)=>{println!("Error getting foo: {e}");return;}};
ifletOk(res)=r.set("foo","bar").await{letres: String =res;println!("{res}");// >>> OK
}else{println!("Error setting foo");}matchr.get("foo").await{Ok(res)=>{letres: String =res;println!("{res}");// >>> bar
},Err(e)=>{println!("Error getting foo: {e}");return;}};

You can also easily store and retrieve a hash:

Foundational: Store and retrieve hash data structures using HSET and HGETALL
lethash_fields=[("model","Deimos"),("brand","Ergonom"),("type","Enduro bikes"),("price","4972"),];ifletOk(res)=r.hset_multiple("bike:1",&hash_fields){letres: String =res;println!("{res}");// >>> OK
}else{println!("Error setting bike:1");}matchr.hget("bike:1","model"){Ok(res)=>{letres: String =res;println!("{res}");// >>> Deimos
},Err(e)=>{println!("Error getting bike:1 model: {e}");return;}}matchr.hget("bike:1","price"){Ok(res)=>{letres: String =res;println!("{res}");// >>> 4972
},Err(e)=>{println!("Error getting bike:1 price: {e}");return;}}matchr.hgetall("bike:1"){Ok(res)=>{letres: Vec<(String,String)>=res;for(key,value)inres{println!("{key}: {value}");}// >>> model: Deimos
// >>> brand: Ergonom
// >>> type: Enduro bikes
// >>> price: 4972
},Err(e)=>{println!("Error getting bike:1: {e}");return;}}
lethash_fields=[("model","Deimos"),("brand","Ergonom"),("type","Enduro bikes"),("price","4972"),];ifletOk(res)=r.hset_multiple("bike:1",&hash_fields).await{letres: String =res;println!("{res}");// >>> OK
}else{println!("Error setting bike:1");}matchr.hget("bike:1","model").await{Ok(res)=>{letres: String =res;println!("{res}");// >>> Deimos
},Err(e)=>{println!("Error getting bike:1 model: {e}");return;}}matchr.hget("bike:1","price").await{Ok(res)=>{letres: String =res;println!("{res}");// >>> 4972
},Err(e)=>{println!("Error getting bike:1 price: {e}");return;}}matchr.hgetall("bike:1").await{Ok(res)=>{letres: Vec<(String,String)>=res;for(key,value)inres{println!("{key}: {value}");}// >>> model: Deimos
// >>> brand: Ergonom
// >>> type: Enduro bikes
// >>> price: 4972
},Err(e)=>{println!("Error getting bike:1: {e}");return;}

More information

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

On this page