理解 &* 来访问 Rust Arc

Gue*_*OCs 3 rust

在https://doc.rust-lang.org/beta/std/sync/struct.Condvar.html上阅读有关 Condvar(Rust 的条件变量)的内容时,我偶然发现:

use std::sync::{Arc, Mutex, Condvar};
use std::thread;

let pair = Arc::new((Mutex::new(false), Condvar::new()));
let pair2 = pair.clone();

// Inside of our lock, spawn a new thread, and then wait for it to start.
thread::spawn(move|| {
    let (lock, cvar) = &*pair2;
    let mut started = lock.lock().unwrap();
    *started = true;
    // We notify the condvar that the value has changed.
    cvar.notify_one();
});

// Wait for the thread to start up.
let (lock, cvar) = &*pair;
let mut started = lock.lock().unwrap();
while !*started {
    started = cvar.wait(started).unwrap();
}
Run Code Online (Sandbox Code Playgroud)

到底是怎么&*pair2回事?我认为这与能够从 内部检索该对有关,但是有一个简单的 检索 的内部对象作为引用Arc不是更好吗?methodArc

有人能给我解释一下具体&*是什么吗?

Lam*_*iry 9

运算*符将Arc<T>变为T。操作&员将其T借入&T.

\n

因此,当我们将它们放在一起时,&*pair借用Arc<T>into &T

\n

编写该代码的另一种方法是:

\n
let (lock, cvar) = pair2.deref();\n
Run Code Online (Sandbox Code Playgroud)\n

事实上,原来的&*pair2实际上意味着&*pair2.deref()\xe2\x80\x93*强制编译器插入一个.deref()调用,并且正是方法执行实际的转换。

\n