Skip to content

Commit cd1bf6d

Browse files
committed
Added short examples for 'str::from_utf8_mut'
1 parent ddd123e commit cd1bf6d

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/libcore/str/mod.rs

+30
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,36 @@ 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+
///
305335
#[stable(feature = "str_mut_extras", since = "1.20.0")]
306336
pub fn from_utf8_mut(v: &mut [u8]) -> Result<&mut str, Utf8Error> {
307337
run_utf8_validation(v)?;

0 commit comments

Comments
 (0)