pub fn set_alloc_error_hook(hook:fn(Layout))alloc_error_hook #51245)Expand description
Registers a custom allocation error hook, replacing any that was previously registered.
The allocation error hook is invoked when an infallible memory allocation fails — that is,as a consequence of callinghandle_alloc_error — before the runtime aborts.
The allocation error hook is a global resource.take_alloc_error_hook may be used toretrieve a previously registered hook and wrap or discard it.
§What the providedhook function should expect
The hook function is provided with aLayout struct which contains informationabout the allocation that failed.
The hook function may choose to panic or abort; in the event that it returns normally, thiswill cause an immediate abort.
Sincetake_alloc_error_hook is a safe function that allows retrieving the hook, the hookfunction must besound to call even if no memory allocations were attempted.
§The default hook
The default hook, used ifset_alloc_error_hook is never called, prints a message tostandard error (and then returns, causing the runtime to abort the process).Compiler options may cause it to panic instead, and the default behavior may be changedto panicking in future versions of Rust.