9951 explained code solutions for 126 technologies


rustHow to send box in Rust


Sending a box in Rust is a simple process. To do so, you must first create a Box type with the Box keyword.

let my_box = Box::new(5);

This creates a box containing the value 5. To send the box, you must use the std::mem::swap function.

let mut target = 0;
std::mem::swap(&mut target, &mut my_box);

This swaps the contents of target and my_box, effectively sending the box.

  1. Create a Box type with the Box keyword: let my_box = Box::new(5);
  2. Use the std::mem::swap function to swap the contents of target and my_box: std::mem::swap(&mut target, &mut my_box);

Helpful links

Edit this code on GitHub