DEV Community

Prashant Sharma
Prashant Sharma

Posted on

How to Iterate Over a String in Rust

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); } } 
Enter fullscreen mode Exit fullscreen mode

Output:

H e l l o , R u s t ! 
Enter fullscreen mode Exit fullscreen mode

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); } } 
Enter fullscreen mode Exit fullscreen mode

Output:

72 101 108 108 111 44 32 82 117 115 116 33 
Enter fullscreen mode Exit fullscreen mode

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" 
Enter fullscreen mode Exit fullscreen mode
use unicode_segmentation::UnicodeSegmentation; fn main() { let my_string = "नमस्ते"; // Example with multi-byte characters for grapheme in my_string.graphemes(true) { println!("{}", grapheme); } } 
Enter fullscreen mode Exit fullscreen mode

Output:

न म स ् ते 
Enter fullscreen mode Exit fullscreen mode

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); } } 
Enter fullscreen mode Exit fullscreen mode

Output:

Index: 0, Character: H Index: 1, Character: e Index: 2, Character: l 
Enter fullscreen mode Exit fullscreen mode

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)