Skip to content

Commit f8428cf

Browse files
committed
doc: add Null-unchecked version section to mut pointer as_mut method
The as_ref method already has a Null-unchecked version section, its example is a modification of the example in the main as_ref section. Similarly the example in this commit is a modification of the example in main as_mut section.
1 parent adc6572 commit f8428cf

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/libcore/ptr/mut_ptr.rs

+14
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,20 @@ impl<T: ?Sized> *mut T {
250250
/// *first_value = 4;
251251
/// println!("{:?}", s); // It'll print: "[4, 2, 3]".
252252
/// ```
253+
///
254+
/// # Null-unchecked version
255+
///
256+
/// If you are sure the pointer can never be null and are looking for some kind of
257+
/// `as_mut_unchecked` that returns the `&mut T` instead of `Option<&mut T>`, know that
258+
/// you can dereference the pointer directly.
259+
///
260+
/// ```
261+
/// let mut s = [1, 2, 3];
262+
/// let ptr: *mut u32 = s.as_mut_ptr();
263+
/// let first_value = unsafe { &mut *ptr };
264+
/// *first_value = 4;
265+
/// println!("{:?}", s); // It'll print: "[4, 2, 3]".
266+
/// ```
253267
#[stable(feature = "ptr_as_ref", since = "1.9.0")]
254268
#[inline]
255269
pub unsafe fn as_mut<'a>(self) -> Option<&'a mut T> {

0 commit comments

Comments
 (0)