Struct Utf8Error
pub struct Utf8Error { /* private fields */ }
An error that occurs when UTF-8 decoding fails.
This error occurs when attempting to convert a non-UTF-8 byte
string to a Rust string that must be valid UTF-8. For example,
to_str is one such method.
Example
This example shows what happens when a given byte sequence is invalid, but ends with a sequence that is a possible prefix of valid UTF-8.
use ;
let s = B;
let err = s.to_str.unwrap_err;
assert_eq!;
assert_eq!;
This example shows what happens when a given byte sequence contains invalid UTF-8.
use ByteSlice;
let s = b"foobar\xF1\x80\x80quux";
let err = s.to_str.unwrap_err;
assert_eq!;
// The error length reports the maximum number of bytes that correspond to
// a valid prefix of a UTF-8 encoded codepoint.
assert_eq!;
// In contrast to the above which contains a single invalid prefix,
// consider the case of multiple individual bytes that are never valid
// prefixes. Note how the value of error_len changes!
let s = b"foobar\xFF\xFFquux";
let err = s.to_str.unwrap_err;
assert_eq!;
assert_eq!;
// The fact that it's an invalid prefix does not change error_len even
// when it immediately precedes the end of the string.
let s = b"foobar\xFF";
let err = s.to_str.unwrap_err;
assert_eq!;
assert_eq!;
Implementations
impl Utf8Error
fn valid_up_to(&self) -> usizeReturns the byte index of the position immediately following the last valid UTF-8 byte.
Example
This examples shows how
valid_up_tocan be used to retrieve a possibly empty prefix that is guaranteed to be valid UTF-8:use ByteSlice; let s = b"foobar\xF1\x80\x80quux"; let err = s.to_str.unwrap_err; // This is guaranteed to never panic. let string = s.to_str.unwrap; assert_eq!;fn error_len(&self) -> Option<usize>Returns the total number of invalid UTF-8 bytes immediately following the position returned by
valid_up_to. This value is always at least1, but can be up to3if bytes form a valid prefix of some UTF-8 encoded codepoint.If the end of the original input was found before a valid UTF-8 encoded codepoint could be completed, then this returns
None. This is useful when processing streams, where aNonevalue signals that more input might be needed.
Trait Implementations
impl Clone for Utf8Error
fn clone(&self) -> Utf8Error
impl Debug for Utf8Error
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Display for Utf8Error
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Eq for Utf8Error
impl Error for Utf8Error
fn description(&self) -> &str
impl PartialEq for Utf8Error
fn eq(&self, other: &Utf8Error) -> bool
impl StructuralPartialEq for Utf8Error
Auto Trait Implementations
impl Freeze for Utf8Error
impl RefUnwindSafe for Utf8Error
impl Send for Utf8Error
impl Sync for Utf8Error
impl Unpin for Utf8Error
impl UnsafeUnpin for Utf8Error
impl UnwindSafe for Utf8Error
Blanket Implementations
impl<T> Any for Utf8Error
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for Utf8Error
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for Utf8Error
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> CloneToUninit for Utf8Error
where
T: Clone,
unsafe fn clone_to_uninit(&self, dest: *mut u8)
impl<T> From<T> for Utf8Error
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for Utf8Error
where
T: Clone,
type Owned = T;fn to_owned(&self) -> Tfn clone_into(&self, target: &mut T)
impl<T> ToString for Utf8Error
where
T: Display + ?Sized,
fn to_string(&self) -> String
impl<T, U> Into<U> for Utf8Error
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for Utf8Error
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for Utf8Error
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>