gpt4 book ai didi

rust - to_string() 导致错误 "borrowed value does not live long enough"

转载 作者:行者123 更新时间:2023-11-29 08:05:20 26 4
gpt4 key购买 nike

为什么 to_string() 导致 borrowed value does not live long enough 错误?示例如下:

use std::collections::HashMap;

struct Foo {
id: Option<usize>,
name: String
}

fn main() {

let foos = getFoos();

for foo in foos {
let mut map = HashMap::new();
map.insert("name", &foo.name);
map.insert("id", &foo.id.unwrap().to_string());
}

}

fn getFoos() -> Vec<Foo> {
Vec::new()
}

错误:

src/main.rs:15:27: 15:54 error: borrowed value does not live long enough
src/main.rs:15 map.insert("id", &foo.id.unwrap().to_string());
^~~~~~~~~~~~~~~~~~~~~~~~~~~
src/main.rs:13:38: 16:6 note: reference must be valid for the block suffix following statement 0 at 13:37...
src/main.rs:13 let mut map = HashMap::new();
src/main.rs:14 map.insert("name", &foo.name);
src/main.rs:15 map.insert("id", &foo.id.unwrap().to_string());
src/main.rs:16 }
src/main.rs:15:9: 15:56 note: ...but borrowed value is only valid for the statement at 15:8
src/main.rs:15 map.insert("id", &foo.id.unwrap().to_string());
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/main.rs:15:9: 15:56 help: consider using a `let` binding to increase its lifetime
src/main.rs:15 map.insert("id", &foo.id.unwrap().to_string());
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

为什么编译器建议创建中间值?这个错误令人困惑。

最佳答案

您正在创建一个保存对字符串的引用的HashMap,即&String。如果我们要注释类型,它看起来像这样:

let mut map: HashMap<&str, &String> = HashMap::new();

这意味着 map 包含大量对生活在其他地方的对象的引用。在您的第一个插入中,它工作得很好,因为 foo.name 存在于其他地方,特别是在对象 foo 中。

map.insert("name", &foo.name);

但是你的第二个插入有一个问题:你想引用一个位于某处的 String 对象。 to_string() 创建一个由函数返回的 String,但在您的情况下它只是一个临时对象。该行执行后对象将被销毁。

map.insert("id", &foo.id.unwrap().to_string());

编译器是对的:let 绑定(bind)可以解决这里的问题。

let mut map = HashMap::new();
map.insert("name", &foo.name);
let id_string = foo.id.unwrap().to_string();
map.insert("id", &id_string);

这在你的小例子中工作得很好,但当你处理更大的事情时它可能会更复杂。例如,如果 HashMap 定义在循环之外,您就会遇到问题,因为您插入 map 的引用需要至少与 map 本身一样长。

关于rust - to_string() 导致错误 "borrowed value does not live long enough",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37387262/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com