Skip to content

Commit b56ff00

Browse files
authored
Rollup merge of rust-lang#44472 - smt923:master, r=frewsxcv
Add short doc examples for str::from_utf8_mut Fixes rust-lang#44462
2 parents 41b0761 + 204414b commit b56ff00

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/libcore/str/mod.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,37 @@ pub fn from_utf8(v: &[u8]) -> Result<&str, Utf8Error> {
302302
}
303303

304304
/// Converts a mutable slice of bytes to a mutable string slice.
305+
///
306+
/// # Examples
307+
///
308+
/// Basic usage:
309+
///
310+
/// ```
311+
/// use std::str;
312+
///
313+
/// // "Hello, Rust!" as a mutable vector
314+
/// let mut hellorust = vec![72, 101, 108, 108, 111, 44, 32, 82, 117, 115, 116, 33];
315+
///
316+
/// // As we know these bytes are valid, we can use `unwrap()`
317+
/// let outstr = str::from_utf8_mut(&mut hellorust).unwrap();
318+
///
319+
/// assert_eq!("Hello, Rust!", outstr);
320+
/// ```
321+
///
322+
/// Incorrect bytes:
323+
///
324+
/// ```
325+
/// use std::str;
326+
///
327+
/// // Some invalid bytes in a mutable vector
328+
/// let mut invalid = vec![128, 223];
329+
///
330+
/// assert!(str::from_utf8_mut(&mut invalid).is_err());
331+
/// ```
332+
/// See the docs for [`Utf8Error`][error] for more details on the kinds of
333+
/// errors that can be returned.
334+
///
335+
/// [error]: struct.Utf8Error.html
305336
#[stable(feature = "str_mut_extras", since = "1.20.0")]
306337
pub fn from_utf8_mut(v: &mut [u8]) -> Result<&mut str, Utf8Error> {
307338
run_utf8_validation(v)?;

0 commit comments

Comments
 (0)