了解结构域突变 [英] Understanding struct-field mutation

查看:153
本文介绍了了解结构域突变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

锈皮书中了解如何对结构字段进行突变:

From the Rust book about how to mutate struct fields:

let mut point = Point { x: 0, y: 0 };
point.x = 5;

及更高版本:

可变性是绑定的属性,而不是结构本身的属性.

Mutability is a property of the binding, not of the structure itself.

这对我来说似乎是违反直觉的,因为point.x = 5看起来不像我在重新绑定变量point.有没有一种方法可以解释这一点,使它更直观?

This seems counter-intuitive to me because point.x = 5 doesn't look like I'm rebinding the variable point. Is there a way to explain this so it's more intuitive?

我能解决这个问题的唯一方法是想象"我将point重新绑定到具有不同x值的原始Point的副本(甚至不确定那是正确的).

The only way I can wrap my head around this is to "imagine" that I'm rebinding point to a copy of the original Point with a different x value (not even sure that's accurate).

推荐答案

我也有同样的困惑.对我来说,它来自两个不同的误解.首先,我来自一种语言,其中变量(aka绑定)是对值的隐式引用.在该语言中,区分突变引用和突变所引用的值非常重要.其次,我以为结构本身"是指实例化的值,但是结构"是指规范/声明,而不是该类型的特定值.

I had the same confusion. For me it came from two separate misunderstandings. First, I came from a language where variables (aka bindings) were implicitly references to values. In that language it was important to distinguish between mutating the reference, and mutating the value that was referred to. Second, I thought by "the structure itself" the book was referring to the instantiated value, but by "the structure" it means the specification/declaration, not a particular value of that type.

Rust中的变量不同.从参考:

Variables in Rust are different. From the reference:

变量是堆栈框架的组成部分...

A variable is a component of a stack frame...

局部变量(或堆栈局部分配)直接保存一个值, 在堆栈的内存中分配.该值是堆栈的一部分 框架.

A local variable (or stack-local allocation) holds a value directly, allocated within the stack's memory. The value is a part of the stack frame.

因此,变量是堆栈框架(内存的一部分)的组成部分,直接由 保留该值.没有引用可以与值本身区分开,也没有引用可以变异.变量和值是相同的内存.

So a variable is a component of a stack frame -- a chunk of memory -- that directly holds the value. There is no reference to distinguish from the value itself, no reference to mutate. The variable and the value are the same hunk of memory.

结果是,重新绑定变量(将其更改为引用不同的内存块)与Rust的内存模型不兼容. (n.b. let x = 1; let x = 2;创建两个变量.)

A consequence is that rebinding a variable in the sense of changing it to refer to a different hunk of memory is not compatible with Rust's memory model. (n.b. let x = 1; let x = 2; creates two variables.)

因此,本书指出,可变性是在每块内存"级别声明的,而不是作为结构定义的一部分.

So the book is pointing out that mutability is declared at the "per hunk of memory" level rather than as part of the definition of a struct.

我唯一能解决这个问题的方法是想象"我正在 将点重新绑定到具有不同x的原始Point的副本 值(甚至不确定该值是否正确)

The only way I can wrap my head around this is to "imagine" that I'm rebinding point to a copy of the original Point with a different x value (not even sure that's accurate)

相反,想像一下您正在将一大堆内存中的0之一更改为5;并且该值位于point指定的内存中.解释绑定是可变的"意味着您可以更改绑定指定的内存块,包括仅更改其一部分,例如通过设置一个结构域.考虑以您描述为在Rust中无法表达的方式重新绑定Rust变量.

Instead imagine you are changing one of the 0's in a hunk of memory to a 5; and that that value resides within the memory designated by point. Interpret "the binding is mutable" to mean that you can mutate the hunk of memory designated by the binding, including mutating just part of it, e.g. by setting a struct field. Think of rebinding Rust variables in the way that you describe as not expressible within Rust.

这篇关于了解结构域突变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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