如何在 Rust 中更改向量中元素的字段? [英] How can I change fields of elements in vectors in Rust?

查看:43
本文介绍了如何在 Rust 中更改向量中元素的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构类型的 vec,我想更改向量中第一个元素的某个字段.我该怎么做?

I have a vec of some struct type and I want to change some field of the first element in the vector. How can I do this?

示例:

struct SomeType {
    some_value: i32,
}

fn main() {
    let mut vec = Vec::new();
    let mut t = SomeType { some_value: 45 };
    vec.push(t);

    println!("Old value: {}", vec.first().unwrap().some_value);
    vec.first().unwrap().some_value += 1;
    println!("New value: {}", vec.first().unwrap().some_value);
}

编译失败:

error: cannot assign to immutable field
  --> vec.rs:15:2
   |
15 |    vec.first().unwrap().some_value += 1;
   |    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot mutably borrow immutable field

我还不能理解 Rust 中的可变性;这里的正确方法是什么?

I can't get my head around the mutability stuff in Rust yet; what would be the correct approach here?

推荐答案

要改变向量的第一个元素,您通常会获得对该元素的引用.换句话说,引用向量.作为一个正常的结构,向量需要有一个方法来为你提供参考.

To mutate the first element of the vector you'd usually get a reference to that element. In other words, a reference into the vector. And being a normal structure the vector needs to have a method that would provide you with the reference.

事实是,对向量的引用意味着您可以对向量的内部进行一些操作,读取它们或以某种方式修改它们.Rust 不知道细节,它只知道当你持有那个引用时,你可以做一些事情.

Thing is, a reference into a vector means you can do something with the insides of the vector, read them or modify them in some way. Rust doesn't know the details, it just knows that while you're holding that reference, you can do stuff.

仅凭这些有限的信息,Rust 的借用检查器就试图阻止您用脚射击自己.它说:如果你要读取向量,很好,你可以用任何你想要的方式读取它,你甚至可以让其他函数读取它,或者两个函数,或者五个.但是你不能在阅读时修改向量,它不安全,它会导致错误.因此,您可以根据需要在向量中拥有任意数量的只读引用,但前提是您没有在其中包含任何可写引用.如果你持有一个可写的引用,那么一次只能有一个这样的引用.

And with just that limited information the borrow checker of Rust tries to stop you from shooting yourself in the foot. It says: if you're going to read the vector, fine, you can read it any way you want, you can even make some other function read it, or two functions, or five. But you can't modify the vector while you're reading it, it isn't safe, it leads to bugs. So, you can have as many read-only references into the vector as you want, but only if and when you're not holding any writeable references into it. If you do hold a writeable reference, then there can be only one such reference at a time.

因此参考的种类很重要.这就是为什么向量有两种方法可以为您提供第一个元素:首先first_mut.

Thus the kind of reference matters. And that is why the vector has the two methods that give you the first element: first and first_mut.

所以在这里

let mut vec = Vec::new();

您的向量已经是可变的.来自其他语言,你可能会凭直觉工作,如果向量是可变的一次,它总是可变的.有点像 C++ 中的 const 值或 D 中的 immutable.它要么是可变的,要么不是.

your vector is already mutable. And coming from other languages you might work from intuition that if the vector is mutable once, it is mutable always. Kind of like const value in C++ or immutable in D. It's either mutable, or it's not.

但在 Rust 中,您可能希望将不可变引用转换为可变结构.例如,您可能希望一个线程处理向量的一个元素,而另一个线程处理另一个元素,如果您希望保持借用检查器的安全带,那么拥有多个引用的最简单方法是保持它们不可变.这就是像 first 这样的方法返回的原因不可变引用,要获得可变引用,您需要使用不同的方法显式选择加入.

But in Rust you might want immutable references into mutable structure. For instance, you might want one thread to work on one element of a vector and another thread on another element, and if you'd rather keep the borrow checker's safety belt on, then the simplest way to have multiple references is to keep them immutable. That is why methods like first return immutable references and to get a mutable reference you need to explicitly opt in by using a different method.

附言那有什么帮助吗?

这篇关于如何在 Rust 中更改向量中元素的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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