[Rust🦀]I analysed the error to make me level up as a Rust developer !!

Rust

# ownership and borrowing

I'm always confused about which valuable has `ownership` or `borrowing`.

The following code was good example to understand the relationship of `ownership` and `borrowing`.

impl Drop for ThreadPool {
    fn drop(&mut self) {
        for worker in &mut self.workers { // 1.
            println!("Shutting down worker {}", worker.id);
            worker.thread.join().unwrap();// 2.
        }
    }
}

This will occur the following error.

```

53 | worker.thread.join().unwrap();
| ^^^^^^^^^^^^^ ------ `worker.thread` moved due to this method call
| |
| move occurs because `worker.thread` has type `JoinHandle<()>`, which does not implement the `Copy` trait

```

This means that the point 1. shows we only borrows Worker instance's pointer.
But the point at 2. shows we try to take ownership to `join()`.
That's why the code won't compile.

コメント

タイトルとURLをコピーしました