about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorsmt923 <smtea923@gmail.com>2017-09-10 04:10:19 +0100
committersmt923 <smtea923@gmail.com>2017-09-10 04:10:19 +0100
commitcd1bf6df4a992d50203385a488cb9ef7251eae5d (patch)
tree8242099203172270cfa87bcc98b0e04abb022678 /src/libcore
parentddd123ed9a35ec76103d42cecf322ee8d2896bf9 (diff)
downloadrust-cd1bf6df4a992d50203385a488cb9ef7251eae5d.tar.gz
rust-cd1bf6df4a992d50203385a488cb9ef7251eae5d.zip
Added short examples for 'str::from_utf8_mut'
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/str/mod.rs30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs
index 62e84c9ebd0..bac3d509c17 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)?;