Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d5bc784

Browse files
authoredJun 8, 2022
Rollup merge of rust-lang#97830 - LucasDumont:add-example-alloc, r=yaahc
Add std::alloc::set_alloc_error_hook example
2 parents 263d868 + 5adef6c commit d5bc784

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)
Please sign in to comment.