根据最后一个元素将某些东西推入向量

问题描述 投票:6回答:1

我想得到一个向量的最后一个元素并用它来确定下一个要推入的元素。这是一个例子,它不起作用,但它显示了我想要实现的目标:

let mut vector: Vec<i32> = Vec::new();

if let Some(last_value) = vector.last() {
    vector.push(*last_value + 1);
}

我也不能使用push,而矢量也是不变地借用的:

error[E0502]: cannot borrow `vector` as mutable because it is also borrowed as immutable
 --> src/main.rs:5:9
  |
4 |     if let Some(last_value) = vector.last() {
  |                               ------ immutable borrow occurs here
5 |         vector.push(*last_value + 1);
  |         ^^^^^^ mutable borrow occurs here
6 |     }
  |     - immutable borrow ends here

这样做有什么好办法?

rust borrow-checker
1个回答
8
投票

After Non-Lexical Lifetimes

您的原始代码works as-is in Rust 2018,它启用non-lexical-lifetimes

fn main() {
    let mut vector: Vec<i32> = Vec::new();

    if let Some(last_value) = vector.last() {
        vector.push(*last_value + 1);
    }
}

借用检查器已得到改进,意识到last_value中的引用与推动新值所需的vector的可变借用不重叠。

请参阅Returning a reference from a HashMap or Vec causes a borrow to last beyond the scope it's in?,了解类似的情况,借用检查器还不够智能(Rust 1.32)。

Before Non-Lexical Lifetimes

vector.last()的结果是Option<&i32>。该值中的引用保持向量借用。在我们推进之前,我们需要摆脱对向量的所有引用。

如果您的向量包含Copyable值,请将该值复制出向量以尽快结束借用。

fn main() {
    let mut vector: Vec<i32> = Vec::new();

    if let Some(&last_value) = vector.last() {
        vector.push(last_value + 1);
    }
}

在这里,我使用了模式Some(&last_value)而不是Some(last_value)。这会破坏参考并强制复制。如果您尝试使用不是Copyable的类型的此模式,您将收到编译器错误:

error[E0507]: cannot move out of borrowed content
 --> src/main.rs:4:17
  |
4 |     if let Some(&last_value) = vector.last() {
  |                 ^----------
  |                 ||
  |                 |hint: to prevent move, use `ref last_value` or `ref mut last_value`
  |                 cannot move out of borrowed content

如果向量不包含Copyable类型,则可能需要首先克隆该值:

fn main() {
    let mut vector: Vec<String> = Vec::new();

    if let Some(last_value) = vector.last().cloned() {
        vector.push(last_value + "abc");
    }
}

或者你可以用另一种方式转换值,使.map()调用返回一个不从向量借用的值。

fn main() {
    let mut vector: Vec<String> = Vec::new();

    if let Some(last_value) = vector.last().map(|v| v.len().to_string()) {
        vector.push(last_value);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.