Open1

Rust , redis 操作する

knaka Tech-Blogknaka Tech-Blog

概要

  • Rust redis 試すメモです。
  • CLI 版です。

[ 公開 2025/06/24 ]


関連

  • install 関連

https://github.com/microsoftarchive/redis/releases


環境

  • redis (windows)
  • rustc 1.87.0
  • cargo 1.87.0
  • ubuntu 22 (windows WSL)

書いたコード

https://gist.github.com/kuc-arc-f/6e1580252ba2c420127acfcf6ca21a72


  • Cargo.toml
[package]
name = "redis_test"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
redis = "0.32"


  • start
cargo build
cargo run

  • src/main.rs
use redis::{Commands, Connection, RedisResult};

fn main() -> RedisResult<()> {
    // Redis に接続
    let client = redis::Client::open("redis://127.0.0.1/")?;
    let mut con = client.get_connection()?;

    // set: キーと値を登録
    let _: () = con.set("name", "Hello, Redis!")?;
    println!("Key saved!");

    // 確認のため get してみる
    let val: String = con.get("name")?;
    println!("my_key => {}", val);

    Ok(())
}

  • SET, GET できました。