Can't find if its a Use-After-Free or Double-Free

⚓ Rust    📅 2026-01-05    👤 surdeus    👁️ 4      

surdeus

Hello, I'm a rust newbie. I was going through the book and I saw the following code block:

use std::thread;

fn main() {
    let v = vec![1, 2, 3];

    let handle = thread::spawn(move || {
        println!("Here's a vector: {v:?}");
    });

    drop(v);

    handle.join().unwrap();
}

If we run we'll get:

   Compiling playground v0.0.1 (/playground)
error[E0382]: use of moved value: `v`
  --> src/main.rs:10:10
   |
 4 |     let v = vec![1, 2, 3];
   |         - move occurs because `v` has type `Vec<i32>`, which does not implement the `Copy` trait
 5 |
 6 |     let handle = thread::spawn(move || {
   |                                ------- value moved into closure here
 7 |         println!("Here's a vector: {v:?}");
   |                                     - variable moved due to use in closure
...
10 |     drop(v);
   |          ^ value used here after move
   |
help: consider cloning the value before moving it into the closure
   |
 6 ~     let value = v.clone();
 7 ~     let handle = thread::spawn(move || {
 8 ~         println!("Here's a vector: {value:?}");
   |

For more information about this error, try `rustc --explain E0382`.
error: could not compile `playground` (bin "playground") due to 1 previous error

The error says ^ value used here after move and If understand correctly its the same as Use-After-Free. but shouldn't it be Double-free because its dropped after when the second thread ends and also with this drop(v);
I think I can't clearly find the difference between those two undefined behaviors.

Thanks!

9 posts - 8 participants

Read full topic

🏷️ Rust_feed