Skip to content

Commit

Permalink
Rollup merge of #44467 - toidiu:ak-44382, r=frewsxcv
Browse files Browse the repository at this point in the history
documentation update to demonstrate mutability

#44467

- demonstrate correct implementation returns `Some`
- demonstrate out of bounds returns `None`
- demonstrate mutability
  • Loading branch information
GuillaumeGomez authored Sep 10, 2017
2 parents dcc1d14 + c430fa8 commit 8a7d93b
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/liballoc/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,16 +390,25 @@ impl str {
/// # Examples
///
/// ```
/// 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());
/// use std::ascii::AsciiExt;
///
/// let mut v = String::from("hello");
/// // correct length
/// assert!(v.get_mut(0..5).is_some());
/// // out of bounds
/// assert!(v.get_mut(..42).is_none());
/// assert_eq!(Some("he"), v.get_mut(0..2).map(|v| &*v));
///
/// assert_eq!("hello", v);
/// {
/// let s = v.get_mut(0..2);
/// let s = s.map(|s| {
/// s.make_ascii_uppercase();
/// &*s
/// });
/// assert_eq!(Some("HE"), s);
/// }
/// assert_eq!("HEllo", v);
/// ```
#[stable(feature = "str_checked_slicing", since = "1.20.0")]
#[inline]
Expand Down

0 comments on commit 8a7d93b

Please sign in to comment.