@@ -36,15 +36,66 @@ pub struct String {
3636 vec : Vec < u8 > ,
3737}
3838
39- /// A possible error value from the `String::from_utf8` function.
39+ /// A possible error value when converting a `String` from a UTF-8 byte vector.
40+ ///
41+ /// This type is the error type for the [`from_utf8()`] method on [`String`]. It
42+ /// is designed in such a way to carefully avoid reallocations: the
43+ /// [`into_bytes()`] method will give back the byte vector that was used in the
44+ /// conversion attempt.
45+ ///
46+ /// [`from_utf8()`]: struct.String.html#method.from_utf8
47+ /// [`String`]: struct.String.html
48+ /// [`into_bytes()`]: struct.FromUtf8Error.html#method.into_bytes
49+ ///
50+ /// The [`Utf8Error`] type provided by [`std::str`] represents an error that may
51+ /// occur when converting a slice of [`u8`]s to a [`&str`]. In this sense, it's
52+ /// an analogue to `FromUtf8Error`, and you can get one from a `FromUtf8Error`
53+ /// through the [`utf8_error()`] method.
54+ ///
55+ /// [`Utf8Error`]: ../str/struct.Utf8Error.html
56+ /// [`std::str`]: ../str/index.html
57+ /// [`u8`]: ../primitive.u8.html
58+ /// [`&str`]: ../primitive.str.html
59+ /// [`utf8_error()`]: #method.utf8_error
60+ ///
61+ /// # Examples
62+ ///
63+ /// Basic usage:
64+ ///
65+ /// ```
66+ /// // some invalid bytes, in a vector
67+ /// let bytes = vec![0, 159];
68+ ///
69+ /// let value = String::from_utf8(bytes);
70+ ///
71+ /// assert!(value.is_err());
72+ /// assert_eq!(vec![0, 159], value.unwrap_err().into_bytes());
73+ /// ```
4074#[ stable( feature = "rust1" , since = "1.0.0" ) ]
4175#[ derive( Debug ) ]
4276pub struct FromUtf8Error {
4377 bytes : Vec < u8 > ,
4478 error : Utf8Error ,
4579}
4680
47- /// A possible error value from the `String::from_utf16` function.
81+ /// A possible error value when converting a `String` from a UTF-16 byte slice.
82+ ///
83+ /// This type is the error type for the [`from_utf16()`] method on [`String`].
84+ ///
85+ /// [`from_utf16()`]: struct.String.html#method.from_utf16
86+ /// [`String`]: struct.String.html
87+ ///
88+ /// # Examples
89+ ///
90+ /// Basic usage:
91+ ///
92+ /// ```
93+ /// // 𝄞mu<invalid>ic
94+ /// let v = &[0xD834, 0xDD1E, 0x006d, 0x0075,
95+ /// 0xD800, 0x0069, 0x0063];
96+ ///
97+ /// assert!(String::from_utf16(v).is_err());
98+ /// ```
4899#[ stable( feature = "rust1" , since = "1.0.0" ) ]
49100#[ derive( Debug ) ]
50101pub struct FromUtf16Error ( ( ) ) ;
@@ -337,13 +388,14 @@ impl String {
337388 ///
338389 /// ```
339390 /// // 𝄞music
340- /// let mut v = &mut [0xD834, 0xDD1E, 0x006d, 0x0075,
341- /// 0x0073, 0x0069, 0x0063];
391+ /// let v = &[0xD834, 0xDD1E, 0x006d, 0x0075,
392+ /// 0x0073, 0x0069, 0x0063];
342393 /// assert_eq!(String::from_utf16(v).unwrap(),
343394 /// "𝄞music".to_string());
344395 ///
345396 /// // 𝄞mu<invalid>ic
346- /// v[4] = 0xD800;
397+ /// let v = &[0xD834, 0xDD1E, 0x006d, 0x0075,
398+ /// 0xD800, 0x0069, 0x0063];
347399 /// assert!(String::from_utf16(v).is_err());
348400 /// ```
349401 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -850,14 +902,52 @@ impl String {
850902}
851903
852904impl FromUtf8Error {
853- /// Consumes this error, returning the bytes that were attempted to make a
854- /// `String` with.
905+ /// Returns the bytes that were attempted to convert to a `String`.
906+ ///
907+ /// This method is carefully constructed to avoid allocation. It will
908+ /// consume the error, moving out the bytes, so that a copy of the bytes
909+ /// does not need to be made.
910+ ///
911+ /// # Examples
912+ ///
913+ /// Basic usage:
914+ ///
915+ /// ```
916+ /// // some invalid bytes, in a vector
917+ /// let bytes = vec![0, 159];
918+ ///
919+ /// let value = String::from_utf8(bytes);
920+ ///
921+ /// assert_eq!(vec![0, 159], value.unwrap_err().into_bytes());
922+ /// ```
855923 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
856924 pub fn into_bytes ( self ) -> Vec < u8 > {
857925 self . bytes
858926 }
859927
860- /// Access the underlying UTF8-error that was the cause of this error.
928+ /// Fetch a `Utf8Error` to get more details about the conversion failure.
929+ ///
930+ /// The [`Utf8Error`] type provided by [`std::str`] represents an error that may
931+ /// occur when converting a slice of [`u8`]s to a [`&str`]. In this sense, it's
932+ /// an analogue to `FromUtf8Error`. See its documentation for more details
933+ /// on using it.
934+ ///
935+ /// [`Utf8Error`]: ../str/struct.Utf8Error.html
936+ /// [`std::str`]: ../str/index.html
937+ ///
938+ /// # Examples
939+ ///
940+ /// Basic usage:
941+ ///
942+ /// ```
943+ /// // some invalid bytes, in a vector
944+ /// let bytes = vec![0, 159];
945+ ///
946+ /// let error = String::from_utf8(bytes).unwrap_err().utf8_error();
947+ ///
948+ /// // the first byte is invalid here
949+ /// assert_eq!(1, error.valid_up_to());
950+ /// ```
861951 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
862952 pub fn utf8_error ( & self ) -> Utf8Error {
863953 self . error
0 commit comments