在哈希图中存储对矢量项的引用时,绑定的寿命不足 [英] Binding does not live long enough when storing a reference to a vector item in a hash map

查看:71
本文介绍了在哈希图中存储对矢量项的引用时,绑定的寿命不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Rust的新手,仍然在借阅检查器方面挣扎,并确保了生命周期的正确性.

I'm new at Rust and still struggling with the borrow checker and getting lifetimes right.

这是我已经开始构建的简单结构-它存储类似东西的命令行参数集合(可以用--string-c或两者表示):

Here's a simple struct I've started to build - it stores collections of command-line argument like things (which can be represented by a --string or a -c or both):

struct OptionMap<'a, T: 'a> {
    name: HashMap<String, &'a T>,
    short_name: HashMap<char, &'a T>,
    options: Vec<T>
}

impl<'a, T: 'a> OptionMap<'a, T> {
    pub fn new() -> OptionMap<'a, T> {
        OptionMap {
            name: HashMap::new(),
            short_name: HashMap::new(),
            options: Vec::new()
        }
    }

    pub fn register(&mut self, name: &OptionName, option: T) {        
        if name.name.is_some() {
            self.name.insert(name.name.unwrap().to_owned(), &option);
        }

        if name.short_name.is_some() {
            self.short_name.insert(name.short_name.unwrap(), &option);
        }

        self.options.push(option);
    }
}

我遇到了两个这样的错误(我写的每个&option参数一个):

I get two of these errors (one for each of the &option parameters I wrote):

   Compiling glam v0.1.0 (file:///Users/carson/Projects/glam)
src/options.rs:57:66: 57:72 error: `option` does not live long enough
src/options.rs:57                 self.name.insert(name.name.unwrap().to_owned(), &option);
                                                                                   ^~~~~~
src/options.rs:54:62: 66:6 note: reference must be valid for the lifetime 'a as defined on the block at 54:61...
src/options.rs:54     pub fn register(&mut self, name: &OptionName, option: T) {
src/options.rs:55         {
src/options.rs:56             if name.name.is_some() {
src/options.rs:57                 self.name.insert(name.name.unwrap().to_owned(), &option);
src/options.rs:58             }
src/options.rs:59         }
                  ...
src/options.rs:54:62: 66:6 note: ...but borrowed value is only valid for the scope of parameters for function at 54:61
src/options.rs:54     pub fn register(&mut self, name: &OptionName, option: T) {
src/options.rs:55         {
src/options.rs:56             if name.name.is_some() {
src/options.rs:57                 self.name.insert(name.name.unwrap().to_owned(), &option);
src/options.rs:58             }
src/options.rs:59         }
                  ...

我传递了对每个哈希映射的引用(因此他们借用了它),然后将选项直接传递给矢量以将其移动到那里,以使该选项不会超出范围.

I pass a reference to each of the hash maps (so they borrow it) and then pass the option straight to the vector to move it there, so that the option doesn't go out of scope.

看来'a的范围和option的范围对我来说应该是相同的-因为OptionMap是使用生存期'a创建的,而T也受该生存期的约束,并且option在函数末尾移入options中.我想念什么?我觉得我一直在为Rust的一生而奋斗,就像有些东西还没有被我点击.

It seems like the scope of 'a and the scope of option should be the same to me - since OptionMap is created with lifetime 'a, and T is bound by that lifetime as well, and option gets moved into options at the end of the function. What am I missing? I feel like I'm constantly fighting with lifetimes in Rust, like there's something that hasn't clicked for me yet.

推荐答案

我传递了对每个哈希映射的引用(因此他们借用了它), 然后将选项直接传递给矢量以将其移动到那里,这样 该选项不会超出范围.

I pass a reference to each of the hash maps (so they borrow it) and then pass the option straight to the vector to move it there, so that the option doesn't go out of scope.

一旦借了东西,就不能将其移到其他地方.

Once something is borrowed, you can't move it elsewhere.

如果将元素放置到向量中并从那里借来,则直到借位结束之前才能对向量进行变异.

If you place an element into a vector and borrow it from there, you can't mutate the vector until the borrow ends.

换句话说,您当前的方法行不通.

In other words your current approach won't work.

最简单的解决方案可能是将索引存储到哈希图中的向量中.

The simplest solution is probably to store indices into the vector in your hash maps.

或者,可以设计一个可以与短名称和长名称进行比较的花式键,然后可以将选项直接存储在单个哈希图中.我说可能",是因为我不确定目前是否可行.

Alternatively, it might be possible to design a fancy key that can be compared to both the short and long names, and then you can store the option directly in a single hash map. I say "might" because I'm not sure if this is currently possible.

这篇关于在哈希图中存储对矢量项的引用时,绑定的寿命不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆