错误[E0597]:借用的值在 While 循环中的生存时间不够长

dre*_*eid 4 rust

我对 Rust 很陌生,我在解决这个错误时遇到了麻烦,但只有当我注释掉 while 语句时才会发生这种情况,基本上我是从控制台询问值并将其存储在 HashMap 中:

use std::collections::HashMap;
use std::io;

fn main() {
    let mut customers = HashMap::new();
    let mut next_customer = true;
    while next_customer {
        let mut input_string = String::new();
        let mut temp_vec = Vec::with_capacity(3);
        let mut vec = Vec::with_capacity(2);
        println!("Insert new customer f.e = customer id,name,address:");
        io::stdin().read_line(&mut input_string);
        input_string = input_string.trim().to_string();
        for s in input_string.split(",") {
            temp_vec.push(s);
        }
        vec.push(temp_vec[1]);
        vec.push(temp_vec[2]);
        let mut key_value = temp_vec[0].parse::<i32>().unwrap();
        customers.insert(key_value, vec);
        next_customer = false;
    }
    println!("DONE");
}
Run Code Online (Sandbox Code Playgroud)

该代码导致错误

error[E0597]: `input_string` does not live long enough
  --> src/main.rs:14:18
   |
14 |         for s in input_string.split(",") {
   |                  ^^^^^^^^^^^^ borrowed value does not live long enough
...
20 |         customers.insert(key_value, vec);
   |         --------- borrow later used here
21 |         next_customer = false;
22 |     }
   |     - `input_string` dropped here while still borrowed
Run Code Online (Sandbox Code Playgroud)

Mic*_*son 6

正如其他人所说,问题在于放入客户地图中的值的生命周期和/或类型。

customers.insert(key_value, vec);
   |         --------- borrow later used here
Run Code Online (Sandbox Code Playgroud)

当编译器决定为对象提供您不期望的类型时,通常会发生这种情况。要了解它在做什么,您可以强制该类型,并查看它如何抱怨。将代码更改为:

    let mut customers: HashMap<(),()> = HashMap::new();
Run Code Online (Sandbox Code Playgroud)

给我们两个相关错误:

20 |         customers.insert(key_value, vec);
   |                          ^^^^^^^^^ expected `()`, found `i32`
...
20 |         customers.insert(key_value, vec);
   |                                     ^^^ expected `()`, found struct `std::vec::Vec`
   |
   = note: expected unit type `()`
                 found struct `std::vec::Vec<&str>`
Run Code Online (Sandbox Code Playgroud)

所以编译器想要给我们客户对象的类型是HashMap<i32, Vec<&str>>

问题是&str生命周期必须在块内,因为我们不将Strings 存储在任何地方,并且它们不能有'static生命周期,因为它们是用户输入。

这意味着我们可能想要一个HashMap<i32,Vec<String>>.

更改代码以使用其中之一会给我们带来一个关于vec类型不正确的错误:它被推导为 a Vec<&str>,但我们想要一个Vec<String>

我们有两个选择。

  1. 在我们使用 将 vec 插入到地图之前,将其转换为正确的类型customers.insert(key_value, vec.iter().map(|s| s.to_string()).collect())。(尽管为了清楚起见,您可能希望将其提取到变量中)。

  2. 显式将 vec 的类型更改为Vec<String>

选项 1“有效”。而选项 2 则引导我们走上一条越来越接近调用的道路,进行类似的更改read_line

一旦您决定在选项 1 中进行修复,如果您发现它们过于嘈杂,则可以删除为解决修复而添加的手动类型注释。