pub struct FromUtf8Error {
bytes: Vec<u8>,
error: Utf8Error,
}Expand description
A possible error value when converting a String from a UTF-8 byte vector.
This type is the error type for the from_utf8 method on String. It
is designed in such a way to carefully avoid reallocations: the
into_bytes method will give back the byte vector that was used in the
conversion attempt.
The Utf8Error type provided by std::str represents an error that may
occur when converting a slice of u8s to a &str. In this sense, it’s
an analogue to FromUtf8Error, and you can get one from a FromUtf8Error
through the utf8_error method.
§Examples
Fields§
§bytes: Vec<u8>§error: Utf8ErrorImplementations§
Source§impl FromUtf8Error
impl FromUtf8Error
1.99.0 · Sourcepub fn into_utf8_lossy(self) -> String
pub fn into_utf8_lossy(self) -> String
Converts the bytes into a String lossily, substituting invalid UTF-8
sequences with replacement characters.
See String::from_utf8_lossy for more details on replacement of
invalid sequences, and String::from_utf8_lossy_owned for the
String function which corresponds to this function.
This is useful in conjunction with String::from_utf8 when you need
to branch on whether the bytes are valid UTF-8, but still want to
recover a lossily converted String in the error case. Use
String::from_utf8_lossy_owned if you always need a lossily converted
String.
Since the original String::from_utf8 error records where validation
stopped, this method does not need to re-check the already valid prefix
of the byte sequence.
§Examples
// some invalid bytes
let input: Vec<u8> = b"Hello \xF0\x90\x80World".into();
let (output, had_invalid_utf8) = match String::from_utf8(input) {
Ok(output) => (output, false),
Err(error) => {
// The bytes were not valid UTF-8, but we can still recover a string.
(error.into_utf8_lossy(), true)
}
};
assert_eq!(String::from("Hello �World"), output);
assert!(had_invalid_utf8);1.0.0 · Sourcepub fn into_bytes(self) -> Vec<u8> ⓘ
pub fn into_bytes(self) -> Vec<u8> ⓘ
Returns the bytes that were attempted to convert to a String.
This method is carefully constructed to avoid allocation. It will consume the error, moving out the bytes, so that a copy of the bytes does not need to be made.
§Examples
1.0.0 · Sourcepub fn utf8_error(&self) -> Utf8Error
pub fn utf8_error(&self) -> Utf8Error
Trait Implementations§
1.0.0 · Source§impl Clone for FromUtf8Error
impl Clone for FromUtf8Error
Source§fn clone(&self) -> FromUtf8Error
fn clone(&self) -> FromUtf8Error
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more1.0.0 · Source§impl Debug for FromUtf8Error
impl Debug for FromUtf8Error
1.0.0 · Source§impl Display for FromUtf8Error
impl Display for FromUtf8Error
1.0.0 · Source§impl Eq for FromUtf8Error
impl Eq for FromUtf8Error
Source§#[doc(hidden)]fn assert_fields_are_eq(&self)
#[doc(hidden)]fn assert_fields_are_eq(&self)
derive_eq_internals)1.0.0 (const: unstable) · Source§#[doc(hidden)]fn assert_receiver_is_total_eq(&self)
#[doc(hidden)]fn assert_receiver_is_total_eq(&self)
implementation detail of #[derive(Eq)]
1.0.0 · Source§impl Error for FromUtf8Error
impl Error for FromUtf8Error
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
Source§#[doc(hidden)]fn type_id(&self, _: Internal) -> TypeIdwhere
Self: 'static,
#[doc(hidden)]fn type_id(&self, _: Internal) -> TypeIdwhere
Self: 'static,
error_type_id #60784)TypeId of self.1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()
1.0.0 · Source§impl PartialEq for FromUtf8Error
impl PartialEq for FromUtf8Error
impl StructuralPartialEq for FromUtf8Error
Auto Trait Implementations§
impl Freeze for FromUtf8Error
impl RefUnwindSafe for FromUtf8Error
impl Send for FromUtf8Error
impl Sync for FromUtf8Error
impl Unpin for FromUtf8Error
impl UnsafeUnpin for FromUtf8Error
impl UnwindSafe for FromUtf8Error
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> SizeHint for Twhere
T: ?Sized,
impl<T> SizeHint for Twhere
T: ?Sized,
Source§default fn lower_bound(&self) -> usize
default fn lower_bound(&self) -> usize
core_io_internals)[u8; 12] could return any value between 0 and
12 inclusively as a correct implementation. Read moreSource§impl<T> SizedTypeProperties for T
impl<T> SizedTypeProperties for T
Source§#[doc(hidden)]const SIZE: usize = _
#[doc(hidden)]const SIZE: usize = _
sized_type_properties)Source§#[doc(hidden)]const ALIGN: usize = _
#[doc(hidden)]const ALIGN: usize = _
sized_type_properties)Source§#[doc(hidden)]const ALIGNMENT: Alignment = _
#[doc(hidden)]const ALIGNMENT: Alignment = _
ptr_alignment_type #102070)Source§#[doc(hidden)]const IS_ZST: bool = _
#[doc(hidden)]const IS_ZST: bool = _
sized_type_properties)Source§#[doc(hidden)]const LAYOUT: Layout = _
#[doc(hidden)]const LAYOUT: Layout = _
sized_type_properties)Source§#[doc(hidden)]const MAX_SLICE_LEN: usize = _
#[doc(hidden)]const MAX_SLICE_LEN: usize = _
sized_type_properties)[Self]. Read more