about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCorey Farwell <coreyf@rwell.org>2017-09-14 22:32:40 -0400
committerGitHub <noreply@github.com>2017-09-14 22:32:40 -0400
commit65b9b564638ab148cf092c0d4cf28d216d58ce92 (patch)
tree1dee10dfcc3470970f4bd322ff2754f53f1af7d8
parentb4f6fba1d9b3acddc8c63e7ccdf857cfc0032aa2 (diff)
parent204414bcf4d3e040c1d3187dfaf6b5781b186ed0 (diff)
downloadrust-65b9b564638ab148cf092c0d4cf28d216d58ce92.tar.gz
rust-65b9b564638ab148cf092c0d4cf28d216d58ce92.zip
Rollup merge of #44472 - smt923:master, r=frewsxcv
Add short doc examples for str::from_utf8_mut

Fixes #44462
-rw-r--r--src/libcore/str/mod.rs31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs
index 62e84c9ebd0..ce331608390 100644
--- a/src/libcore/str/mod.rs
+++ b/src/libcore/str/mod.rs
@@ -302,6 +302,37 @@ 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.
+///
+/// [error]: struct.Utf8Error.html
 #[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)?;