< 返回版块

1148118271 发表于 2021-09-24 15:51

Tags:解引用

pub async fn get_list() -> Json<Results<String>> {
    let x = handle::execute("query<->blog<->column<->[*]").unwrap();
    let v = x.1.get(0).unwrap();
    let v = *v;
    let string = TeraEntity::render("list", &Context::new()).unwrap();
    let results = Results {
        code: 200,
        msg: "成功".to_string(),
        data: string
    };
    Json(results)
}
error[E0507]: cannot move out of `*v` which is behind a shared reference
  --> src/methods/index.rs:53:13
   |
53 |     let v = *v;
   |             ^^
   |             |
   |             move occurs because `*v` has type `HashMap<std::string::String, std::string::String>`, which does not implement the `Copy` trait
   |             help: consider borrowing here: `&*v`

warning: unused import: `Read`

v这个变量不能被解引用

评论区

写评论
Easonzero 2021-09-24 16:46

不考虑clone的话可以remove,会返回有所有权的v

eweca-d 2021-09-24 16:35

那就CLONE呗

--
👇
1148118271: v 那个hashMap已经是借用类型了

 let v: &HashMap<String, String> = x.1.get(0).unwrap();

我有没有什么办法直接获取 v 这个对象的所有权

作者 1148118271 2021-09-24 16:14

v 那个hashMap已经是借用类型了

 let v: &HashMap<String, String> = x.1.get(0).unwrap();

我有没有什么办法直接获取 v 这个对象的所有权

matrikslee 2021-09-24 16:07

编译器给的提醒已经很清楚了啦 move occurs because *v has type HashMap<std::string::String, std::string::String>, which does not implement the Copy trait

hashmap 没有copy trait所以不能使用移动语义,而且编译器得建议也很清楚哦,考虑使用借用

1 共 4 条评论, 1 页