Skip to content

Commit 329ed4a

Browse files
Rollup merge of rust-lang#44457 - napen123:master, r=frewsxcv
Add doc examples for str::as_bytes_mut Fixes rust-lang#44427
2 parents 485847d + 6c89935 commit 329ed4a

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

β€Žsrc/liballoc/str.rs

+28
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,34 @@ impl str {
297297
/// [`str::from_utf8_mut`] function.
298298
///
299299
/// [`str::from_utf8_mut`]: ./str/fn.from_utf8_mut.html
300+
///
301+
/// # Examples
302+
///
303+
/// Basic usage:
304+
///
305+
/// ```
306+
/// let mut s = String::from("Hello");
307+
/// let bytes = unsafe { s.as_bytes_mut() };
308+
///
309+
/// assert_eq!(b"Hello", bytes);
310+
/// ```
311+
///
312+
/// Mutability:
313+
///
314+
/// ```
315+
/// let mut s = String::from("πŸ—»βˆˆπŸŒ");
316+
///
317+
/// unsafe {
318+
/// let bytes = s.as_bytes_mut();
319+
///
320+
/// bytes[0] = 0xF0;
321+
/// bytes[1] = 0x9F;
322+
/// bytes[2] = 0x8D;
323+
/// bytes[3] = 0x94;
324+
/// }
325+
///
326+
/// assert_eq!("πŸ”βˆˆπŸŒ", s);
327+
/// ```
300328
#[stable(feature = "str_mut_extras", since = "1.20.0")]
301329
#[inline(always)]
302330
pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {

0 commit comments

Comments
Β (0)