问题描述
尽管通读了文档,但对&
和 *
符号的含义感到困惑
Despite thoroughly reading the documentation, I'm rather confused about the meaning of the &
and *
symbol in Rust, and more generally about what is a Rust reference exactly.
在此示例中,它似乎类似于C ++引用(即,一个地址
In this example, it seems to be similar to a C++ reference (that is, an address that is automatically dereferenced when used):
fn main() {
let c: i32 = 5;
let rc = &c;
let next = rc + 1;
println!("{}", next); // 6
}
但是,以下代码完全相同:
However, the following code works exactly the same:
fn main() {
let c: i32 = 5;
let rc = &c;
let next = *rc + 1;
println!("{}", next); // 6
}
使用 *
取消引用的引用在C ++中是不正确的。所以我想了解为什么在Rust中这是正确的。
Using *
to dereference a reference wouldn't be correct in C++. So I'd like to understand why this is correct in Rust.
到目前为止,我的理解是,插入 * $ c $在Rust引用前面的c>取消了对它的引用,但是
*
仍被隐式插入,因此您无需添加它(在C ++中,它是隐式插入的,如果
My understanding so far, is that, inserting *
in front of a Rust reference dereferences it, but the *
is implicitly inserted anyway so you don't need to add it (while in C++, it's implicitly inserted and if you insert it you get a compilation error).
但是,这样的东西不能编译:
However, something like this doesn't compile:
fn main() {
let mut c: i32 = 5;
let mut next: i32 = 0;
{
let rc = &mut c;
next = rc + 1;
}
println!("{}", next);
}
error[E0369]: binary operation `+` cannot be applied to type `&mut i32`
--> src/main.rs:6:16
|
6 | next = rc + 1;
| ^^^^^^
|
= note: this is a reference to a type that `+` can be applied to; you need to dereference this variable once for this operation to work
= note: an implementation of `std::ops::Add` might be missing for `&mut i32`
但这可行:
fn main() {
let mut c: i32 = 5;
let mut next: i32 = 0;
{
let rc = &mut c;
next = *rc + 1;
}
println!("{}", next); // 6
}
似乎隐式取消引用(la C ++)对于不变的引用,但不能用于可变引用。为什么会这样?
It seems that implicit dereferencing (a la C++) is correct for immutable references, but not for mutable references. Why is this?
推荐答案
C ++中的引用与Rust中的引用不同。 Rust的引用(在用法上,而不是语义上)与C ++的指针更接近。关于内存表示,Rust的引用通常只是一个指针,而C ++的引用应该是同一对象的替代名称(因此没有内存表示)。
A reference in C++ is not the same as a reference in Rust. Rust's references are much closer (in usage, not in semantics) to C++'s pointers. With respect to memory representation, Rust's references often are just a single pointer, while C++'s references are supposed to be alternative names of the same object (and thus have no memory representation).
C ++指针与Rust引用之间的区别在于,Rust的引用永远不会 NULL
,永远不会被初始化和悬空。
The difference between C++ pointers and Rust references is that Rust's references are never NULL
, never uninitialized and never dangling.
特征用于以下对和所有其他数字基元(请参见文档页面的底部):
The Add
trait is implemented (see the bottom of the doc page) for the following pairs and all other numeric primitives:
-
& i32
+i32
-
i32
+& i32
-
& ; i32
+& i32
&i32
+i32
i32
+&i32
&i32
+&i32
此这只是std-lib开发人员实现的一项便捷操作。编译器可以确定在可以使用& i32
的任何地方都可以使用& mut i32
,但是对于泛型而言,这还行不通(还?),因此std-lib开发人员还需要为以下组合(以及所有原语的组合)实现 Add
特征:
This is just a convenience thing the std-lib developers implemented. The compiler can figure out that a &mut i32
can be used wherever a &i32
can be used, but that doesn't work (yet?) for generics, so the std-lib developers would need to also implement the Add
traits for the following combinations (and those for all primitives):
-
& mut i32
+i32
-
i32
+& mut i32
-
& mut i32
+& mut i32
-
& mut i32
+& i32
-
& i32
+& mut i32
&mut i32
+i32
i32
+&mut i32
&mut i32
+&mut i32
&mut i32
+&i32
&i32
+&mut i32
如您所见,这可能会完全失控。我相信将来会消失。在此之前,请注意,以& mut i32
结尾并尝试在数学表达式中使用它是很少见的。
As you can see that can get quite out of hand. I'm sure that will go away in the future. Until then, note that it's rather rare to end up with a &mut i32
and trying to use it in a mathematical expression.
这篇关于&符的含义和Rust中的星号“ *”符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!