IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: 1.1.0
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

BytesIter

struct BytesIter[mut: Bool, //, origin: Origin[mut=mut]]

Iterator over the raw UTF-8 bytes of a string slice, constructed by StringSlice.bytes().

Each call to __next__() yields the next Byte value from the underlying UTF-8 encoded data. Unlike CodepointsIter and GraphemeSliceIter, this iterator operates at the byte level and does not interpret multi-byte UTF-8 sequences as codepoints or grapheme clusters.

Parameters​

  • ​mut (Bool): Whether the underlying string data is mutable.
  • ​origin (Origin[mut=mut]): The origin of the underlying string data.

Implemented traits​

AnyType, Copyable, Deinitable, ImplicitlyCopyable, Iterable, Iterator, Movable, Sized

comptime members​

Element​

comptime Element = Byte

The element type yielded by iteration.

IteratorType​

comptime IteratorType[iterable_mut: Bool, //, iterable_origin: Origin[mut=iterable_mut]] = BytesIter[origin]

The iterator type for this bytes iterator.

Parameters​

Methods​

__iter__​

def __iter__(ref self) -> Self

Iterator over the underlying string's bytes.

Returns:

Self: This iterator.

__next__​

def __next__(mut self) -> Byte

Get the next byte in the underlying string slice.

Returns:

Byte: The next byte in the string.

Raises:

StopIteration if the iterator has been exhausted.

bounds​

def bounds(self) -> Tuple[Int, Optional[Int]]

Returns bounds [lower, upper] for the remaining iterator length.

Returns:

Tuple[Int, Optional[Int]]: The lower and upper bound of this iterator.

__len__​

def __len__(self) -> Int

Returns the remaining length of this iterator in bytes.

Returns:

Int: Number of bytes remaining in this iterator.