Struct Utf8Chunks

#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct Utf8Chunks<'a> { pub(in ::str::lossy) source: &'a [u8] }

An iterator used to decode a slice of mostly UTF-8 bytes to string slices ([&str]) and byte slices (&[u8]).

This struct is created by the utf8_chunks method on bytes slices. If you want a simple conversion from UTF-8 byte slices to string slices, from_utf8 is easier to use.

See the Utf8Chunk type for documentation of the items yielded by this iterator.

Examples

This can be used to create functionality similar to String::from_utf8_lossy without allocating heap memory:

fn from_utf8_lossy<F>(input: &[u8], mut push: F) where F: FnMut(&str) {
    for chunk in input.utf8_chunks() {
        push(chunk.valid());

        if !chunk.invalid().is_empty() {
            push("\u{FFFD}");
        }
    }
}

Fields

source: &'a [u8]

Implementations

impl<'a> Utf8Chunks<'a>

fn debug(&self) -> Debug<'_>

Trait Implementations

impl Debug for Utf8Chunks<'_>

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl FusedIterator for Utf8Chunks<'_>

impl<'a> Clone for Utf8Chunks<'a>

fn clone(&self) -> Utf8Chunks<'a>

impl<'a> Iterator for Utf8Chunks<'a>

type Item = Utf8Chunk<'a>;
fn next(&mut self) -> Option<Utf8Chunk<'a>>

Auto Trait Implementations

impl<'a> Freeze for Utf8Chunks<'a>

impl<'a> RefUnwindSafe for Utf8Chunks<'a>

impl<'a> Send for Utf8Chunks<'a>

impl<'a> Sync for Utf8Chunks<'a>

impl<'a> Unpin for Utf8Chunks<'a>

impl<'a> UnsafeUnpin for Utf8Chunks<'a>

impl<'a> UnwindSafe for Utf8Chunks<'a>

Blanket Implementations

impl<I> IntoIterator for Utf8Chunks<'a> where I: Iterator,

type Item = <I as Iterator>::Item;
type IntoIter = I;
fn into_iter(self) -> I

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

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Utf8Chunks<'a> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Utf8Chunks<'a> where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> CloneToUninit for Utf8Chunks<'a> where T: Clone,

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> From<T> for Utf8Chunks<'a>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> SizeHint for Utf8Chunks<'a> where T: ?Sized,

fn lower_bound(&self) -> usize
fn upper_bound(&self) -> Option<usize>

impl<T> SizedTypeProperties for Utf8Chunks<'a>

impl<T, U> Into<U> for Utf8Chunks<'a> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for Utf8Chunks<'a> where U: Into<T>,

type Error = Infallible;
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto<U> for Utf8Chunks<'a> where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>