Movatterモバイル変換


[0]ホーム

URL:


set_alloc_error_hook

std::alloc

Functionset_alloc_error_hook 

Source
pub fn set_alloc_error_hook(hook:fn(Layout))
🔬This is a nightly-only experimental API. (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.

§Examples

#![feature(alloc_error_hook)]usestd::alloc::{Layout, set_alloc_error_hook};fncustom_alloc_error_hook(layout: Layout) {panic!("memory allocation of {} bytes failed", layout.size());}set_alloc_error_hook(custom_alloc_error_hook);

[8]ページ先頭

©2009-2026 Movatter.jp