Skip to content

Commit 5a5a32a

Browse files
committed
Auto merge of #43652 - frewsxcv:frewsxcv-str-examples, r=QuietMisdreavus
String slice doc improvements. None
2 parents 2b82b7e + 6722185 commit 5a5a32a

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

src/liballoc/str.rs

+32-11
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,10 @@ impl str {
273273
core_str::StrExt::is_char_boundary(self, index)
274274
}
275275

276-
/// Converts a string slice to a byte slice.
276+
/// Converts a string slice to a byte slice. To convert the byte slice back
277+
/// into a string slice, use the [`str::from_utf8`] function.
278+
///
279+
/// [`str::from_utf8`]: ./str/fn.from_utf8.html
277280
///
278281
/// # Examples
279282
///
@@ -289,7 +292,11 @@ impl str {
289292
core_str::StrExt::as_bytes(self)
290293
}
291294

292-
/// Converts a mutable string slice to a mutable byte slice.
295+
/// Converts a mutable string slice to a mutable byte slice. To convert the
296+
/// mutable byte slice back into a mutable string slice, use the
297+
/// [`str::from_utf8_mut`] function.
298+
///
299+
/// [`str::from_utf8_mut`]: ./str/fn.from_utf8_mut.html
293300
#[stable(feature = "str_mut_extras", since = "1.20.0")]
294301
#[inline(always)]
295302
pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {
@@ -328,11 +335,16 @@ impl str {
328335
/// # Examples
329336
///
330337
/// ```
331-
/// let v = "🗻∈🌏";
338+
/// let mut v = String::from("🗻∈🌏");
339+
///
332340
/// assert_eq!(Some("🗻"), v.get(0..4));
333-
/// assert!(v.get(1..).is_none());
334-
/// assert!(v.get(..8).is_none());
335-
/// assert!(v.get(..42).is_none());
341+
///
342+
/// // indices not on UTF-8 sequence boundaries
343+
/// assert!(v.get_mut(1..).is_none());
344+
/// assert!(v.get_mut(..8).is_none());
345+
///
346+
/// // out of bounds
347+
/// assert!(v.get_mut(..42).is_none());
336348
/// ```
337349
#[stable(feature = "str_checked_slicing", since = "1.20.0")]
338350
#[inline]
@@ -351,9 +363,14 @@ impl str {
351363
///
352364
/// ```
353365
/// let mut v = String::from("🗻∈🌏");
366+
///
354367
/// assert_eq!(Some("🗻"), v.get_mut(0..4).map(|v| &*v));
368+
///
369+
/// // indices not on UTF-8 sequence boundaries
355370
/// assert!(v.get_mut(1..).is_none());
356371
/// assert!(v.get_mut(..8).is_none());
372+
///
373+
/// // out of bounds
357374
/// assert!(v.get_mut(..42).is_none());
358375
/// ```
359376
#[stable(feature = "str_checked_slicing", since = "1.20.0")]
@@ -563,12 +580,16 @@ impl str {
563580
/// Basic usage:
564581
///
565582
/// ```
566-
/// let mut s = "Per Martin-Löf".to_string();
567-
///
568-
/// let (first, last) = s.split_at_mut(3);
583+
/// use std::ascii::AsciiExt;
569584
///
570-
/// assert_eq!("Per", first);
571-
/// assert_eq!(" Martin-Löf", last);
585+
/// let mut s = "Per Martin-Löf".to_string();
586+
/// {
587+
/// let (first, last) = s.split_at_mut(3);
588+
/// first.make_ascii_uppercase();
589+
/// assert_eq!("PER", first);
590+
/// assert_eq!(" Martin-Löf", last);
591+
/// }
592+
/// assert_eq!("PER Martin-Löf", s);
572593
/// ```
573594
#[inline]
574595
#[stable(feature = "str_split_at", since = "1.4.0")]

0 commit comments

Comments
 (0)