参考和盒子之间的可变性差异 [英] Difference in mutability between reference and box

查看:76
本文介绍了参考和盒子之间的可变性差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解Rust指针类型及其与可变性的关系.具体来说,声明一个变量的方法,该变量持有指针并且本身是可变的-即可以指向其他内存,并且声明数据本身是可变的-即可以更改通过指针变量的值.

I'm trying to understand Rust pointer types and their relation to mutability. Specifically, the ways of declaring a variable which holds the pointer and is itself mutable -- i.e. can be pointed to some other memory, and declaring that the data itself is mutable -- i.e. can be changed through the value of the pointer variable.

这就是我理解普通引用工作的方式:

This is how I understand plain references work:

let mut a = &5; // a is a mutable pointer to immutable data
let b = &mut 5; // b is an immutable pointer to mutable data

因此, a 可以更改为指向其他内容,而 b 则不能.但是, b 指向的数据可以通过 b 进行更改,而不能通过 a 进行更改.我是否正确理解?

So a can be changed to point to something else, while b can't. However, the data to which b points to can be changed through b, while it can't through a. Do I understand this correctly?

问题的第二部分-为什么 Box :: new 的行为似乎有所不同?这是我目前的理解:

For the second part of the question -- why does Box::new seem to behave differently? This is my current understanding:

let mut a = Box::new(5); // a is a mutable pointer to mutable data
let c = Box::new(7); // c is an immutable pointer to immutable data

new 应该返回一个指向一些堆分配数据的指针,但是它指向的数据似乎继承了保存该指针的变量的可变性,这与带有两个状态的引用示例不同可变性是独立的! Box :: new 应该如何工作?如果可以,如何在存储在不可变变量中的堆上​​创建指向可变数据的指针值?

new should return a pointer to some heap-allocated data, but the data it points to seems to inherit mutability from the variable which holds the pointer, unlike in the example with references where these two states of mutability are independent! Is that how Box::new is supposed to work? If so, how can I create a pointer value to mutable data on the heap that is stored in an immutable variable?

推荐答案

首先,您确实了解引用的行为方式. mut a 是可变变量(或更准确地说,是可变绑定),而& mut 5 是指向可变数据段(即为您隐式分配在堆栈上).

First, you do understand how references behave correctly. mut a is a mutable variable (or, more correctly, a mutable binding), while &mut 5 is a mutable reference pointing to a mutable piece of data (which is implicitly allocated on the stack for you).

其次, Box 的行为与参考不同,因为它与参考有根本的不同. Box 的另一个名称是拥有/拥有的指针.每个 Box 拥有它拥有的数据,并且唯一地拥有它们,因此,此数据的可变性是从Box本身的可变性继承的.是的,这正是 Box 的工作方式.

Second, Box behaves differently from references because it is fundamentally different from references. Another name for Box is owning/owned pointer. Each Box owns the data it holds, and it does so uniquely, therefore mutability of this data is inherited from mutability of the box itself. So yes, this is exactly how Box should work.

理解它的另一种可能更实用的方法是考虑 Box< T> 完全等同于 T ,除了固定大小和分配方法外.换句话说, Box 提供了值语义:它就像任何值一样移动,其可变性取决于它存储在其中的绑定.

Another, probably more practical, way to understand it is to consider Box<T> exactly equivalent to just T, except of fixed size and allocation method. In other words, Box provides value semantics: it is moved around just like any value and its mutability depends on the binding it is stored in.

有多种方法可以创建指向堆上可变数据的指针,同时保持该指针不变.最通用的一种是 RefCell :

There are several ways to create a pointer to a mutable piece of data on the heap while keeping the pointer immutable. The most generic one is RefCell:

use std::cell::RefCell;

struct X { id: u32 }
let x: Box<RefCell<X>> = Box::new(RefCell::new(X { id: 0 }));
x.borrow_mut().id = 1;

或者,您可以使用 Cell (对于 Copy 类型):

Alternatively, you can use Cell (for Copy types):

let x: Box<Cell<u32>> = Box::new(Cell::new(0));
x.set(1);

请注意,上述示例使用的是内部可变性",除非您确实需要使用它,否则最好避免使用.如果您想创建一个内部可变的 Box 仅用于保留可变性属性,则实际上不应该.它不是惯用语言,只会导致语法和语义负担.

Note that the above examples are using so-called "internal mutability" which should better be avoided unless you do need it for something. If you want to create a Box with mutable interior only to keep mutability properties, you really shouldn't. It isn't idiomatic and will only result in a syntactic and semantic burden.

您可以在此处找到很多有用的信息:

You can find a lot of useful information here:

  • Ownership
  • References and borrowing
  • Mutability
  • std::cell - internal mutability types

实际上,如果您对可变性等基本问题有疑问,则可能已经在书中对此进行了解释:)

In fact, if you have a question about such fundamental things as mutability, it is probably already explained in the book :)

这篇关于参考和盒子之间的可变性差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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