本文介绍了&符号'&'的含义并在 Rust 中标出“*"符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管彻底阅读了文档,但我对 Rust 中 &* 符号的含义以及更一般的关于什么是 Rust 引用感到困惑正是.

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.

到目前为止,我的理解是,在 Rust 引用前插入 * 会取消引用它,但是 * 无论如何都是隐式插入的,因此您不需要添加它(而在 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
}

似乎隐式解除引用(a 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.

Add trait为以下对和所有其他数字原语实现(参见文档页面底部):

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 开发人员实现的一个方便的东西.编译器可以确定 &mut i32 可以用于任何可以使用 &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 中标出“*"符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-16 18:38