about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2022-06-08 13:32:19 -0700
committerGitHub <noreply@github.com>2022-06-08 13:32:19 -0700
commit888d72c2bfd1f3755ee370e83f3248efa0689fc6 (patch)
tree295042aa72c0c5d7ca0b19b2634d0d01aec9a31c
parent1922f0b9802a2edee8bacc3639f651b82d38edd1 (diff)
parent5adef6c79598bc7dbfa3882739a6e508559cae42 (diff)
downloadrust-888d72c2bfd1f3755ee370e83f3248efa0689fc6.tar.gz
rust-888d72c2bfd1f3755ee370e83f3248efa0689fc6.zip
Rollup merge of #97830 - LucasDumont:add-example-alloc, r=yaahc
Add std::alloc::set_alloc_error_hook example
-rw-r--r--library/std/src/alloc.rs14
1 files changed, 14 insertions, 0 deletions
diff --git a/library/std/src/alloc.rs b/library/std/src/alloc.rs
index 63c527b64da..d3879273f5b 100644
--- a/library/std/src/alloc.rs
+++ b/library/std/src/alloc.rs
@@ -296,6 +296,20 @@ static HOOK: AtomicPtr<()> = AtomicPtr::new(ptr::null_mut());
 /// about the allocation that failed.
 ///
 /// The allocation error hook is a global resource.
+///
+/// # Examples
+///
+/// ```
+/// #![feature(alloc_error_hook)]
+///
+/// use std::alloc::{Layout, set_alloc_error_hook};
+///
+/// fn custom_alloc_error_hook(layout: Layout) {
+///    panic!("memory allocation of {} bytes failed", layout.size());
+/// }
+///
+/// set_alloc_error_hook(custom_alloc_error_hook);
+/// ```
 #[unstable(feature = "alloc_error_hook", issue = "51245")]
 pub fn set_alloc_error_hook(hook: fn(Layout)) {
     HOOK.store(hook as *mut (), Ordering::SeqCst);