Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Rust

Idiom #286 Iterate over characters of a string

Print a line "Char i is c" for each character c of the string s, where i is the character index of c in s (not the byte index).

Make sure that multi-byte characters are properly handled, and count for a single character.

for (i, c) in s.chars().enumerate() {
    println!("Char {} is {}", i, c);
}

s may have type &str or String.
c has type char.
(dorun (map-indexed (fn [i c] (println "Char" i "is" c)) s))

In Clojure a string is a list of characters

New implementation...