1

https://doc.rust-lang.org/std/sync/struct.Condvar.html#examples

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

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

// 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();
}

Why isn't 17th line written like this:

let (lock, cvar) = pair;

Or like this:

let (lock, cvar) = *pair;

Or somehow else

Wynell
  • 633
  • 1
  • 8
  • 15

1 Answers1

4

The asterisk makes Arc<(Mutex<bool>, Condvar)> become (Mutex<bool>, Condvar). Then the ampersand makes it become a &(Mutex<bool>, Condvar).

So as a result, lock's type is &Mutex<bool> and cvar's type is &Condvar.

Now, let's see why we need the & by trying to remove it:

let (lock, cvar) = *pair;

The compiler explains the problem:

error[E0507]: cannot move out of an `Arc`
  --> src/main.rs:26:24
   |
26 |     let (lock, cvar) = *pair;
   |          ----  ----    ^^^^^ help: consider borrowing here: `&*pair`
   |          |     |
   |          |     ...and here
   |          data moved here
   |
   = note: move occurs because these variables have types that don't implement the `Copy` trait
at54321
  • 8,726
  • 26
  • 46