DEV Community

Prashant Sharma
Prashant Sharma

Posted on

1

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.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay