The code

Generate a new crate

Let’s start by generating a new Rust app:

  1. $ cargo new my-redis
  2. $ cd my-redis

Add dependencies

Next, open Cargo.toml and add the following right below [dependencies]:

  1. tokio = { version = "1", features = ["full"] }
  2. mini-redis = "0.4"

Write the code

Then, open main.rs and replace the contents of the file with:

  1. use mini_redis::{client, Result};
  2. #[tokio::main]
  3. pub async fn main() -> Result<()> {
  4. // Open a connection to the mini-redis address.
  5. let mut client = client::connect("127.0.0.1:6379").await?;
  6. // Set the key "hello" with value "world"
  7. client.set("hello", "world".into()).await?;
  8. // Get key "hello"
  9. let result = client.get("hello").await?;
  10. println!("got value from the server; result={:?}", result);
  11. Ok(())
  12. }

Make sure the Mini-Redis server is running. In a separate terminal window, run:

  1. $ mini-redis-server

Now, run the my-redis application:

  1. $ cargo run
  2. got value from the server; result=Some(b"world")

Success!

You can find the full code here.