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