From cd1bf6df4a992d50203385a488cb9ef7251eae5d Mon Sep 17 00:00:00 2001 From: smt923 Date: Sun, 10 Sep 2017 04:10:19 +0100 Subject: [PATCH] Added short examples for 'str::from_utf8_mut' --- src/libcore/str/mod.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index 62e84c9ebd01..bac3d509c171 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -302,6 +302,36 @@ pub fn from_utf8(v: &[u8]) -> Result<&str, Utf8Error> { } /// Converts a mutable slice of bytes to a mutable string slice. +/// +/// # Examples +/// +/// Basic usage: +/// +/// ``` +/// use std::str; +/// +/// // "Hello, Rust!" as a mutable vector +/// let mut hellorust = vec![72, 101, 108, 108, 111, 44, 32, 82, 117, 115, 116, 33]; +/// +/// // As we know these bytes are valid, we can use `unwrap()` +/// let outstr = str::from_utf8_mut(&mut hellorust).unwrap(); +/// +/// assert_eq!("Hello, Rust!", outstr); +/// ``` +/// +/// # Incorrect bytes: +/// +/// ``` +/// use std::str; +/// +/// // Some invalid bytes in a mutable vector +/// let mut invalid = vec![128, 223]; +/// +/// assert!(str::from_utf8_mut(&mut invalid).is_err()); +/// ``` +/// See the docs for [`Utf8Error`][error] for more details on the kinds of +/// errors that can be returned. +/// #[stable(feature = "str_mut_extras", since = "1.20.0")] pub fn from_utf8_mut(v: &mut [u8]) -> Result<&mut str, Utf8Error> { run_utf8_validation(v)?;