What exactly is the difference between Box::into_raw() and Box::leak()?

Viewed 78

As you know, both Box::into_raw() and Box::leak() consume the current Box and lose ownership of the memory.

The two just seem to have different types of return values, what exactly is the other difference between them?

How about typical application scenarios?

1 Answers

into_raw is typically used for FFI to get a pointer that can be sent to the other language, and is usually matched with a later call to from_raw to reclaim ownership and free the memory.

leak is typically used to get a 'static reference to satisfy some API requirement and is usually kept until the program exits.

Related