什么时候应该在赋值的两边使用“&”?

When should "&" be used on both sides of an assignment?

Condvar 文档显示了一个包含以下内容的示例:

let pair = Arc::new((Mutex::new(false), Condvar::new()));
// <snipped for brevity>
let &(ref lock, ref cvar) = &*pair;

我想知道在此作业的两边都包含 & 可能有什么好处。换句话说,为什么不直接写:

let (ref lock, ref cvar) = *pair;

两个版本都可以编译;有什么语义上的区别吗?如果不是,是否有任何理由更喜欢示例中出现的语法?

总的来说,我仍在努力理解 when/where &* 应该以惯用的方式出现在 Rust 代码中 。我的直觉是 "see"(读取)这个字符组合作为空操作,尽管我知道由于 Rust 的 Deref 语义,情况并非如此。

在这种情况下使用 & 没有区别,因为 LHS 是 destructuring pattern。在我看来,这个例子不应该使用&。然而,在其他情况下有一个重要的区别:* 移动延迟值:

let p = *pair; // *Deref::deref(pair)
// pair cannot be used anymore because the deferred value was moved

let p = &*pair; // &*Deref::deref(pair)
// p: &(Mutex::new(false), Condvar::new())
// p and pair can be used

I'm still struggling to understand when/where &* should idiomatically appear in Rust code.

通常,当需要引用延迟值时使用 &*,例如在函数调用中:

fn sum(a: &u32, b: &u32) -> u32 {
   a + b
}

fn main() {
    let x = Box::new(10);
    // println!("{:?}", sum(&1, x)); // do not work x is Box<u32>
    // println!("{:?}", sum(&1, *x)); // do not work *x is u32
    println!("{:?}", sum(&1, &*x)); // ok &*x is &u32
}

My instinct is to "see" (read) this character combination as a no-op, though I understand that because of Rust's Deref semantics this isn't the case.

考虑到 The Book 说“Deref 对编写自定义指针类型很有用”,我喜欢将 &* 读作 "a reference to the value that this (smart) pointer points at"。