@@ -36,15 +36,66 @@ pub struct String {
36
36
vec : Vec < u8 > ,
37
37
}
38
38
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
+ /// ```
40
74
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
41
75
#[ derive( Debug ) ]
42
76
pub struct FromUtf8Error {
43
77
bytes : Vec < u8 > ,
44
78
error : Utf8Error ,
45
79
}
46
80
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
+ /// ```
48
99
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
49
100
#[ derive( Debug ) ]
50
101
pub struct FromUtf16Error ( ( ) ) ;
@@ -337,13 +388,14 @@ impl String {
337
388
///
338
389
/// ```
339
390
/// // 𝄞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];
342
393
/// assert_eq!(String::from_utf16(v).unwrap(),
343
394
/// "𝄞music".to_string());
344
395
///
345
396
/// // 𝄞mu<invalid>ic
346
- /// v[4] = 0xD800;
397
+ /// let v = &[0xD834, 0xDD1E, 0x006d, 0x0075,
398
+ /// 0xD800, 0x0069, 0x0063];
347
399
/// assert!(String::from_utf16(v).is_err());
348
400
/// ```
349
401
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -850,14 +902,52 @@ impl String {
850
902
}
851
903
852
904
impl 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
+ /// ```
855
923
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
856
924
pub fn into_bytes ( self ) -> Vec < u8 > {
857
925
self . bytes
858
926
}
859
927
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
+ /// ```
861
951
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
862
952
pub fn utf8_error ( & self ) -> Utf8Error {
863
953
self . error
0 commit comments