How to Use Redis with Rust

Connect to Redis in Rust using the redis crate with tokio for async operations to store and retrieve key-value pairs.

Use the redis crate to connect to a Redis server and execute commands via an async client.

[dependencies]
redis = { version = "0.27", features = ["tokio-comp"] }
tokio = { version = "1", features = ["full"] }
use redis::Client;

#[tokio::main]
async fn main() -> redis::RedisResult<()> {
    let client = Client::open("redis://127.0.0.1/")?;
    let mut con = client.get_async_connection().await?;
    redis::cmd("SET").arg("my_key").arg("my_value").query_async(&mut con).await?;
    let val: String = redis::cmd("GET").arg("my_key").query_async(&mut con).await?;
    println!("Value: {val}");
    Ok(())
}