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.
- Create a
Boxtype with theBoxkeyword:let my_box = Box::new(5); - Use the
std::mem::swapfunction to swap the contents oftargetandmy_box:std::mem::swap(&mut target, &mut my_box);
Helpful links
Related
More of Rust
- How to get execution time in Rust
- How to iterate over a Rust slice with an index?
- How to borrow from vector in Rust
- How to match whitespace with a regex in Rust?
- How to find the first match in a Rust regex?
- How to iterate an array with index in Rust
- How to calculate the inverse of a matrix in Rust?
- How to use a BuildHasher in Rust?
- Example box expression in Rust
- How do I create a class variable in Rust?
See more codes...