Movatterモバイル変換


[0]ホーム

URL:


Utf8Error

std::str

StructUtf8Error 

1.0.0 ·Source
pub struct Utf8Error {/* private fields */ }
Expand description

Errors which can occur when attempting to interpret a sequence ofu8as a string.

As such, thefrom_utf8 family of functions and methods for bothStringsand&strs make use of this error, for example.

§Examples

This error type’s methods can be used to create functionalitysimilar toString::from_utf8_lossy without allocating heap memory:

fnfrom_utf8_lossy<F>(mutinput:&[u8],mutpush: F)whereF: FnMut(&str) {loop{matchstd::str::from_utf8(input) {Ok(valid) => {                push(valid);break}Err(error) => {let(valid, after_valid) = input.split_at(error.valid_up_to());unsafe{                    push(std::str::from_utf8_unchecked(valid))                }                push("\u{FFFD}");if letSome(invalid_sequence_length) = error.error_len() {                    input =&after_valid[invalid_sequence_length..]                }else{break}            }        }    }}

Implementations§

Source§

implUtf8Error

1.5.0 (const: 1.63.0) ·Source

pub const fnvalid_up_to(&self) ->usize

Returns the index in the given string up to which valid UTF-8 wasverified.

It is the maximum index such thatfrom_utf8(&input[..index])would returnOk(_).

§Examples

Basic usage:

usestd::str;// some invalid bytes, in a vectorletsparkle_heart =vec![0,159,146,150];// std::str::from_utf8 returns a Utf8Errorleterror = str::from_utf8(&sparkle_heart).unwrap_err();// the second byte is invalid hereassert_eq!(1, error.valid_up_to());
1.20.0 (const: 1.63.0) ·Source

pub const fnerror_len(&self) ->Option<usize>

Provides more information about the failure:

  • None: the end of the input was reached unexpectedly.self.valid_up_to() is 1 to 3 bytes from the end of the input.If a byte stream (such as a file or a network socket) is being decoded incrementally,this could be a validchar whose UTF-8 byte sequence is spanning multiple chunks.

  • Some(len): an unexpected byte was encountered.The length provided is that of the invalid byte sequencethat starts at the index given byvalid_up_to().Decoding should resume after that sequence(after inserting aU+FFFD REPLACEMENT CHARACTER) in case oflossy decoding.

Trait Implementations§

1.0.0 ·Source§

implClone forUtf8Error

Source§

fnclone(&self) ->Utf8Error

Returns a duplicate of the value.Read more
1.0.0 ·Source§

fnclone_from(&mut self, source: &Self)

Performs copy-assignment fromsource.Read more
1.0.0 ·Source§

implDebug forUtf8Error

Source§

fnfmt(&self, f: &mutFormatter<'_>) ->Result<(),Error>

Formats the value using the given formatter.Read more
1.0.0 ·Source§

implDisplay forUtf8Error

Source§

fnfmt(&self, f: &mutFormatter<'_>) ->Result<(),Error>

Formats the value using the given formatter.Read more
1.0.0 ·Source§

implError forUtf8Error

1.30.0 ·Source§

fnsource(&self) ->Option<&(dynError + 'static)>

Returns the lower-level source of this error, if any.Read more
1.0.0 ·Source§

fndescription(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 ·Source§

fncause(&self) ->Option<&dynError>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fnprovide<'a>(&'a self, request: &mutRequest<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access #99301)
Provides type-based access to context intended for error reports.Read more
1.0.0 ·Source§

implPartialEq forUtf8Error

Source§

fneq(&self, other: &Utf8Error) ->bool

Tests forself andother values to be equal, and is used by==.
1.0.0 ·Source§

fnne(&self, other:&Rhs) ->bool

Tests for!=. The default implementation is almost always sufficient,and should not be overridden without very good reason.
1.0.0 ·Source§

implCopy forUtf8Error

1.0.0 ·Source§

implEq forUtf8Error

1.0.0 ·Source§

implStructuralPartialEq forUtf8Error

Auto Trait Implementations§

§

implFreeze forUtf8Error

§

implRefUnwindSafe forUtf8Error

§

implSend forUtf8Error

§

implSync forUtf8Error

§

implUnpin forUtf8Error

§

implUnwindSafe forUtf8Error

Blanket Implementations§

Source§

impl<T>Any for T
where T: 'static + ?Sized,

Source§

fntype_id(&self) ->TypeId

Gets theTypeId ofself.Read more
Source§

impl<T>Borrow<T> for T
where T: ?Sized,

Source§

fnborrow(&self) ->&T

Immutably borrows from an owned value.Read more
Source§

impl<T>BorrowMut<T> for T
where T: ?Sized,

Source§

fnborrow_mut(&mut self) ->&mut T

Mutably borrows from an owned value.Read more
Source§

impl<T>CloneToUninit for T
where T:Clone,

Source§

unsafe fnclone_to_uninit(&self, dest:*mutu8)

🔬This is a nightly-only experimental API. (clone_to_uninit #126799)
Performs copy-assignment fromself todest.Read more
Source§

impl<T>From<T> for T

Source§

fnfrom(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U>Into<U> for T
where U:From<T>,

Source§

fninto(self) -> U

CallsU::from(self).

That is, this conversion is whatever the implementation ofFrom<T> for U chooses to do.

Source§

impl<T>ToOwned for T
where T:Clone,

Source§

typeOwned = T

The resulting type after obtaining ownership.
Source§

fnto_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning.Read more
Source§

fnclone_into(&self, target:&mut T)

Uses borrowed data to replace owned data, usually by cloning.Read more
Source§

impl<T>ToString for T
where T:Display + ?Sized,

Source§

fnto_string(&self) ->String

Converts the given value to aString.Read more
Source§

impl<T, U>TryFrom<U> for T
where U:Into<T>,

Source§

typeError =Infallible

The type returned in the event of a conversion error.
Source§

fntry_from(value: U) ->Result<T, <T asTryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U>TryInto<U> for T
where U:TryFrom<T>,

Source§

typeError = <U asTryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fntry_into(self) ->Result<U, <U asTryFrom<T>>::Error>

Performs the conversion.

[8]ページ先頭

©2009-2026 Movatter.jp