Skip to content

String slice doc improvements. #43652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 5, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions src/liballoc/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,10 @@ impl str {
core_str::StrExt::is_char_boundary(self, index)
}

/// Converts a string slice to a byte slice.
/// Converts a string slice to a byte slice. To convert the byte slice back
/// into a string slice, use the [`str::from_utf8`] function.
///
/// [`str::from_utf8`]: ./str/fn.from_utf8.html
///
/// # Examples
///
Expand All @@ -289,7 +292,11 @@ impl str {
core_str::StrExt::as_bytes(self)
}

/// Converts a mutable string slice to a mutable byte slice.
/// Converts a mutable string slice to a mutable byte slice. To convert the
/// mutable byte slice back into a mutable string slice, use the
/// [`str::from_utf8_mut`] function.
///
/// [`str::from_utf8_mut`]: ./str/fn.from_utf8_mut.html
#[stable(feature = "str_mut_extras", since = "1.20.0")]
#[inline(always)]
pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {
Expand Down Expand Up @@ -328,11 +335,16 @@ impl str {
/// # Examples
///
/// ```
/// let v = "🗻∈🌏";
/// let mut v = String::from("🗻∈🌏");
///
/// assert_eq!(Some("🗻"), v.get(0..4));
/// assert!(v.get(1..).is_none());
/// assert!(v.get(..8).is_none());
/// assert!(v.get(..42).is_none());
///
/// // indices not on UTF-8 sequence boundaries
/// assert!(v.get_mut(1..).is_none());
/// assert!(v.get_mut(..8).is_none());
///
/// // out of bounds
/// assert!(v.get_mut(..42).is_none());
/// ```
#[stable(feature = "str_checked_slicing", since = "1.20.0")]
#[inline]
Expand All @@ -351,9 +363,14 @@ impl str {
///
/// ```
/// let mut v = String::from("🗻∈🌏");
///
/// assert_eq!(Some("🗻"), v.get_mut(0..4).map(|v| &*v));
///
/// // indices not on UTF-8 sequence boundaries
/// assert!(v.get_mut(1..).is_none());
/// assert!(v.get_mut(..8).is_none());
///
/// // out of bounds
/// assert!(v.get_mut(..42).is_none());
/// ```
#[stable(feature = "str_checked_slicing", since = "1.20.0")]
Expand Down Expand Up @@ -563,12 +580,16 @@ impl str {
/// Basic usage:
///
/// ```
/// let mut s = "Per Martin-Löf".to_string();
///
/// let (first, last) = s.split_at_mut(3);
/// use std::ascii::AsciiExt;
///
/// assert_eq!("Per", first);
/// assert_eq!(" Martin-Löf", last);
/// let mut s = "Per Martin-Löf".to_string();
/// {
/// let (first, last) = s.split_at_mut(3);
/// first.make_ascii_uppercase();
/// assert_eq!("PER", first);
/// assert_eq!(" Martin-Löf", last);
/// }
/// assert_eq!("PER Martin-Löf", s);
/// ```
#[inline]
#[stable(feature = "str_split_at", since = "1.4.0")]
Expand Down