Skip to content

Commit 0508329

Browse files
committed
Rollup merge of #27521 - steveklabnik:doc_std_mem_forget, r=gankro
We were burying the reason to use this function below a bunch of caveats about its usage. That's backwards. Why a function should be used belongs at the top of the docs, not the bottom. Also, add some extra links to related functions mentioned in the body. /cc @abhijeetbhagat who pointed this out on IRC
2 parents b1dc2c5 + 5af1b3f commit 0508329

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

src/libcore/mem.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ pub use intrinsics::transmute;
2929
/// `mem::drop` function in that it **does not run the destructor**, leaking the
3030
/// value and any resources that it owns.
3131
///
32+
/// There's only a few reasons to use this function. They mainly come
33+
/// up in unsafe code or FFI code.
34+
///
35+
/// * You have an uninitialized value, perhaps for performance reasons, and
36+
/// need to prevent the destructor from running on it.
37+
/// * You have two copies of a value (like when writing something like
38+
/// [`mem::swap`][swap]), but need the destructor to only run once to
39+
/// prevent a double `free`.
40+
/// * Transferring resources across [FFI][ffi] boundries.
41+
///
42+
/// [swap]: fn.swap.html
43+
/// [ffi]: ../../book/ffi.html
44+
///
3245
/// # Safety
3346
///
3447
/// This function is not marked as `unsafe` as Rust does not guarantee that the
@@ -52,20 +65,9 @@ pub use intrinsics::transmute;
5265
/// * `mpsc::{Sender, Receiver}` cycles (they use `Arc` internally)
5366
/// * Panicking destructors are likely to leak local resources
5467
///
55-
/// # When To Use
56-
///
57-
/// There's only a few reasons to use this function. They mainly come
58-
/// up in unsafe code or FFI code.
59-
///
60-
/// * You have an uninitialized value, perhaps for performance reasons, and
61-
/// need to prevent the destructor from running on it.
62-
/// * You have two copies of a value (like `std::mem::swap`), but need the
63-
/// destructor to only run once to prevent a double free.
64-
/// * Transferring resources across FFI boundries.
65-
///
6668
/// # Example
6769
///
68-
/// Leak some heap memory by never deallocating it.
70+
/// Leak some heap memory by never deallocating it:
6971
///
7072
/// ```rust
7173
/// use std::mem;
@@ -74,7 +76,7 @@ pub use intrinsics::transmute;
7476
/// mem::forget(heap_memory);
7577
/// ```
7678
///
77-
/// Leak an I/O object, never closing the file.
79+
/// Leak an I/O object, never closing the file:
7880
///
7981
/// ```rust,no_run
8082
/// use std::mem;
@@ -84,7 +86,7 @@ pub use intrinsics::transmute;
8486
/// mem::forget(file);
8587
/// ```
8688
///
87-
/// The swap function uses forget to good effect.
89+
/// The `mem::swap` function uses `mem::forget` to good effect:
8890
///
8991
/// ```rust
9092
/// use std::mem;

0 commit comments

Comments
 (0)