Copy big value from Box into Vec in Rust without blowing the stack

Viewed 202

I want to copy (not move) a very big value from a box into a vec. The normal way of doing this (dereferencing the box) means the value is copied onto the stack temporarily, which blows it. Here's an example and a Playground link where it can be reproduced.

fn main() {
    let big_value = Box::new([0u8; 8 * 1024 * 1024]);
    let mut vec = Vec::new();
    vec.push(*big_value);
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=5998cff185dc209e7a1e676d41850ff4

Since both the Box and the Vec are on the heap, it should be possible to do a copy without passing through the stack. What's the best solution here?

1 Answers
Related