progquester
progquester

Reputation: 1766

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

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?

Upvotes: 7

Views: 4455

Answers (1)

Jmb
Jmb

Reputation: 23264

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.

Upvotes: 10

Related Questions