gpt4 book ai didi

struct - 在以下上下文中如何解决 "lifetime of reference outlives lifetime of borrowed content"?

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

我的 lib.rs 中有以下结构

pub enum ConfigurationSource {
StringContent(String),
FileContent(PathBuf)
}

pub struct ConfigurationBuilder<'a> {
config: Value,
bundles: HashMap<&'a str, &'a Vec<ConfigurationSource>>
}

impl<'a> ConfigurationBuilder<'a>{
pub fn new(base_source: &ConfigurationSource) -> ConfigurationBuilder{
let base_config: Value = from_str("{}").unwrap();

let mut config_builder = ConfigurationBuilder{
config: base_config,
bundles: HashMap::new()
};

config_builder.merge_source(&base_source);

return config_builder;
}

//more code here

pub fn define_bundle(&mut self, bundle_key: &str, sources: &Vec<ConfigurationSource>){
self.bundles.insert(bundle_key, sources);
}
}

我不希望 ConfigurationBuilder 实例中的 bundle Hashmap 拥有传递给 define_bundle 方法的 bundle_keysource

我在构建时遇到以下两个编译错误

error[E0312]: lifetime of reference outlives lifetime of borrowed content...
--> src\lib.rs:67:41
|
67 | self.bundles.insert(bundle_key, sources);
| ^^^^^^^
|
note: ...the reference is valid for the lifetime 'a as defined on the impl at 27:1...
--> src\lib.rs:27:1
|
27 | / impl<'a> ConfigurationBuilder<'a>{
28 | |
29 | | pub fn new(base_source: &ConfigurationSource) -> ConfigurationBuilder{
30 | | let base_config: Value = from_str("{}").unwrap();
... |
89 | | }
90 | | }
| |_^
note: ...but the borrowed content is only valid for the anonymous lifetime #3 defined on the method
body at 66:5
--> src\lib.rs:66:5
|
66 | / pub fn define_bundle(&mut self, bundle_key: &str, sources: &Vec<ConfigurationSource>){
67 | | self.bundles.insert(bundle_key, sources);
68 | | }
| |_____^

error[E0312]: lifetime of reference outlives lifetime of borrowed content...
--> src\lib.rs:67:29
|
67 | self.bundles.insert(bundle_key, sources);
| ^^^^^^^^^^
|
note: ...the reference is valid for the lifetime 'a as defined on the impl at 27:1...
--> src\lib.rs:27:1
|
27 | / impl<'a> ConfigurationBuilder<'a>{
28 | |
29 | | pub fn new(base_source: &ConfigurationSource) -> ConfigurationBuilder{
30 | | let base_config: Value = from_str("{}").unwrap();
... |
89 | | }
90 | | }
| |_^
note: ...but the borrowed content is only valid for the anonymous lifetime #2 defined on the method
body at 66:5
--> src\lib.rs:66:5
|
66 | / pub fn define_bundle(&mut self, bundle_key: &str, sources: &Vec<ConfigurationSource>){
67 | | self.bundles.insert(bundle_key, sources);
68 | | }
| |_____^

我做错了什么?

最佳答案

define_bundle 的输入参数的生命周期编译器不知道是否满足或超过为结构定义的参数。

告诉编译器他们期望相同的生命周期就可以了:

pub fn define_bundle(&mut self, bundle_key: &'a str, sources: &'a Vec<ConfigurationSource>) {
// ^^----------------^^ same lifetimes as the struct fields expect
self.bundles.insert(bundle_key, sources);
}

关于struct - 在以下上下文中如何解决 "lifetime of reference outlives lifetime of borrowed content"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47248268/

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