How to get the number of characters in a string, not bytes, in Rust?
Hello, colleagues! I've just started learning Rust and encountered an unexpected problem. I need to find the length of a string in characters (char), not in bytes. In other languages, such as Python or Java, the len() method usually returns the number of characters. But in Rust, it's different.
Here's an example that confused me:
```rust
fn main() {
let text = String::from("Привет, мир!");
println!("String length: {}", text.len());
}
```
I expected to see 12 (the number of characters in the string "Привет, мир!"), but got 20. Then I realized that the .len() method counts bytes. In UTF-8, Cyrillic characters take up 2 bytes, so the result is different.
But I need exactly the number of characters — for a user interface, input length validation, or word counting. Please tell me, is there any