In Rust, strings (String
and &str
) are UTF-8 encoded, which means characters can be multiple bytes long. This makes direct indexing impossible, but Rust provides safe and efficient ways to iterate over strings. Here’s how you can do it:
1. Iterate Over Characters
To iterate through each character (char
) in a string:
fn main() { let my_string = "Hello, Rust!"; for c in my_string.chars() { println!("{}", c); } }
Output:
H e l l o , R u s t !
2. Iterate Over Bytes
To work with the raw bytes of a string, use the bytes
method:
fn main() { let my_string = "Hello, Rust!"; for b in my_string.bytes() { println!("{}", b); } }
Output:
72 101 108 108 111 44 32 82 117 115 116 33
3. Iterate Over Grapheme Clusters
To handle user-perceived characters (like emojis or accented characters), use the unicode-segmentation
crate:
[dependencies] unicode-segmentation = "1.10"
use unicode_segmentation::UnicodeSegmentation; fn main() { let my_string = "नमस्ते"; // Example with multi-byte characters for grapheme in my_string.graphemes(true) { println!("{}", grapheme); } }
Output:
न म स ् ते
4. Iterate with Indices
To get both the index and the character:
fn main() { let my_string = "Hello, Rust!"; for (index, c) in my_string.char_indices() { println!("Index: {}, Character: {}", index, c); } }
Output:
Index: 0, Character: H Index: 1, Character: e Index: 2, Character: l
Summary
-
chars
: Iterate over individual characters. -
bytes
: Iterate over raw bytes. -
graphemes
: Work with user-perceived characters. -
char_indices
: Iterate with indices.
Top comments (0)