ScalarIterator
Traverses one valid UTF-8 view as Unicode scalar values.
Module std.iteration
import std.iteration;Public declaration contracts and authored examples. A page can document several related symbols. Implementation bodies are excluded.
ScalarIterator
pub final class ScalarIteratorTraverses one valid UTF-8 view as Unicode scalar values.
The cursor borrows the original byte storage through a shrinking str view, allocates nothing, and performs one bounded UTF-8 decode per item. Its constructor/factory/step also support required constant evaluation when the borrowed input is evaluator-owned immutable UTF-8 storage.
Example
module example.scalar_iterator;
fn Main() -> i32 {
var Cursor = "Aλ".CreateIterator();
var Count = 0;
while Cursor.Next() is .Some { Value = _ } {
Count += 1;
}
// Direct range syntax uses the same authenticated scalar-cursor protocol.
var DirectCount = 0;
for Scalar in "Aλ" { DirectCount += 1; }
for Scalar in "" { return 1; }
if DirectCount != Count { return 2; }
return Count - 2;
}ScalarIterator.fn
pub const fn Next() -> char?Decodes and consumes the next Unicode scalar. Return: The next scalar by value, or none when no bytes remain. Warning: The cursor remains valid only while the original backing storage remains live and unmoved.