< 返回版块

1911860538 发表于 2024-02-01 11:18

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,如果换成无关生命周期的类型,就没问题

评论区

写评论
作者 1911860538 2024-02-01 13:58
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_a_strs(&self) -> Vec<String> {
        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 {
            vs.push(v);
        }
        vs
    }

    fn strs_as(a_strs: &Vec<String>) -> Vec<A> {
        let mut a_s = Vec::with_capacity(a_strs.len());
        for b in a_strs {
            let a: A = serde_json::from_str(b).unwrap();
            a_s.push(a);
        }
        a_s
    }

    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 b_s = logic.load_a_strs();
    let a_s = Logic::strs_as(&b_s);
    println!("a_s is {:?}", a_s);
    logic.clear_as();
}

--
👇
Bai-Jinlin: 你的代码和下面的又有什么区别呢,报错都一样

fn foo<'a>()->&'a i32{
    let a=1;
    &a
}
Bai-Jinlin 2024-02-01 11:26

你的代码和下面的又有什么区别呢,报错都一样

fn foo<'a>()->&'a i32{
    let a=1;
    &a
}
1 共 2 条评论, 1 页