首页 > 解决方案 > 如何获得持久的`Vec.last()` 指针?

问题描述

我正在尝试用 Rust 构建一个虚拟机,但我遇到了(我认为是)一个基本的生命周期问题。

以下是VM的相关部分:

#[derive(Copy, Clone)]
enum Value<'v> {
    Bool(bool),
    Str(&'v str),
    Empty,
}

// Updated VM
struct VM<'vm> {
    values: Vec<Value<'vm>>,

    // `VM.strings` will keep references to "result" strings, like those from `concat`.
    strings: Vec<String>,
}

impl<'vm> VM<'vm> {
    // Pushes the given value onto the stack.
    // Returns the index of the value.
    fn push_value(&mut self, value: Value<'vm>) -> usize {
        self.values.push(value);
        return self.values.len() - 1;
    }

    fn get_value(&self, index: usize) -> Value<'vm> {
        // Assume the best of our users...
        return unsafe { *self.values.get_unchecked(index) };
    }

    fn concat(&mut self, i1: usize, i2: usize) -> usize {
        let first = self.get_value(i1);
        let second = self.get_value(i2);
        let result = match (first, second) {
            (Value::Str(v1), Value::Str(v2)) => [v1, v2].concat(),
            _ => panic!("Attempted to CONCAT non-strings."),
        };

        self.strings.push(result);
        let result_ptr = self.strings.last().unwrap();
        self.push_value(Value::Str(result_ptr))
    }
}

我正在尝试concat为两个字符串编写一个操作。这是我的尝试:

Rust 编译器给了我一个我不完全理解的详细错误:

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements
  --> src/lib.rs:38:39
   |
38 |         let result_ptr = self.strings.last().unwrap();
   |                                       ^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 29:5...
  --> src/lib.rs:29:5
   |
29 | /     fn concat(&mut self, i1: usize, i2: usize) -> usize {
30 | |         let first = self.get_value(i1);
31 | |         let second = self.get_value(i2);
32 | |         let result = match (first, second) {
...  |
39 | |         self.push_value(Value::Str(result_ptr))
40 | |     }
   | |_____^
note: ...so that reference does not outlive borrowed content
  --> src/lib.rs:38:26
   |
38 |         let result_ptr = self.strings.last().unwrap();
   |                          ^^^^^^^^^^^^
note: but, the lifetime must be valid for the lifetime `'vm` as defined on the impl at 16:6...
  --> src/lib.rs:16:6
   |
16 | impl<'vm> VM<'vm> {
   |      ^^^
note: ...so that the types are compatible
  --> src/lib.rs:39:14
   |
39 |         self.push_value(Value::Str(result_ptr))
   |              ^^^^^^^^^^
   = note: expected  `&mut VM<'_>`
              found  `&mut VM<'vm>`

一些杂七杂八的想法:

标签: rustlifetime

解决方案


推荐阅读