When you run this program, you’ll see output similar to:
This Rust code demonstrates similar concepts to the original example:
It shows how strings are stored as UTF-8 encoded bytes.
It demonstrates iterating over a string’s bytes and characters.
It shows how to get the byte length and character count of a string.
It demonstrates how to work with individual characters (chars) in a string.
It includes an example of passing a char to a function and comparing it with literals.
The main differences are in the syntax and the specific methods used. For example, Rust uses chars() instead of utf8.RuneCountInString(), and char_indices() instead of ranging over the string directly. The concept of “runes” in Go is replaced by Rust’s char type, which represents a Unicode scalar value.