std-method

str::char_indices

Yield pairs of byte offset and Unicode scalar. Use the offsets to construct valid slice boundaries. Expected stdout (not executed): 0: A 1: ش 3: B Common mistake: Reading the first field as a sequential character number.

Formal Syntax & Structure

rust syntaxrust
fn char_indices(&self) -> CharIndices<'_>

Working Example

Example Snippetrust
fn main() {    for (i, c) in "AشB".char_indices() { println!("{i}: {c}"); }}
Rust Reference