use redis::{Client, Commands, Connection, Iter};
use serde::{Deserialize, Serialize};
const HASH_KEY: &str = "rust_test_hash_key";
#[derive(Serialize, Deserialize, Debug)]
struct A<'a> {
id: &'a str,
}
struct Logic {
redis: Client,
}
impl Logic {
fn new() -> Self {
let redis_client =
Client::open("redis://@127.0.0.1:6378/10").unwrap();
Logic {
redis: redis_client,
}
}
fn get_redis_conn(&self) -> Connection {
self.redis.get_connection().unwrap()
}
fn store_as(&self) {
let mut conn = self.get_redis_conn();
let field = "field_name";
let a = A { id: "id0" };
let value = serde_json::to_string(&a).unwrap();
let _: () = conn.hset(HASH_KEY, field, value).unwrap();
}
fn load_as(&self) -> Vec<A> {
let mut conn = self.get_redis_conn();
let results: Iter<(String, String)> = conn.hscan(HASH_KEY).unwrap();
let mut vs = Vec::new();
for (_k, v) in results {
let a: A = serde_json::from_str(&v).unwrap();
vs.push(a);
}
vs
}
fn clear_as(&self) {
let mut conn = self.get_redis_conn();
let _: () = conn.del(HASH_KEY).unwrap();
}
}
fn main() {
let logic = Logic::new();
logic.store_as();
let a_s = logic.load_as();
println!("a_s is {:?}", a_s);
logic.clear_as();
}
具体报错信息如下
error[E0515]: cannot return value referencing local variable `v`
--> src/main.rs:2051:9
|
2048 | let a: A = serde_json::from_str(&v).unwrap();
| -- `v` is borrowed here
...
2051 | vs
| ^^ returns a value referencing data owned by the current function
发现根源是结构体A,id为&'a str,如果换成无关生命周期的类型,就没问题
1
共 2 条评论, 1 页
评论区
写评论--
👇
Bai-Jinlin: 你的代码和下面的又有什么区别呢,报错都一样
你的代码和下面的又有什么区别呢,报错都一样