Skip to content

Commit 5adef6c

Browse files
committed
Add std::alloc::set_alloc_error_hook example
1 parent 91cacb3 commit 5adef6c

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

library/std/src/alloc.rs

+14
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,20 @@ static HOOK: AtomicPtr<()> = AtomicPtr::new(ptr::null_mut());
296296
/// about the allocation that failed.
297297
///
298298
/// The allocation error hook is a global resource.
299+
///
300+
/// # Examples
301+
///
302+
/// ```
303+
/// #![feature(alloc_error_hook)]
304+
///
305+
/// use std::alloc::{Layout, set_alloc_error_hook};
306+
///
307+
/// fn custom_alloc_error_hook(layout: Layout) {
308+
/// panic!("memory allocation of {} bytes failed", layout.size());
309+
/// }
310+
///
311+
/// set_alloc_error_hook(custom_alloc_error_hook);
312+
/// ```
299313
#[unstable(feature = "alloc_error_hook", issue = "51245")]
300314
pub fn set_alloc_error_hook(hook: fn(Layout)) {
301315
HOOK.store(hook as *mut (), Ordering::SeqCst);

0 commit comments

Comments
 (0)