How to get entire utf8 char?

โš“ Rust    ๐Ÿ“… 2025-12-01    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 2      

surdeus

Hi,

I compare two String char by char.
There are utf8 string.

In the documentation, for char_indices function on String, we have this example to access the char at some index.
But in fact, it is not the entire char... it is some part of the encoded char.

let yes = "yฬ†es";

let mut char_indices = yes.char_indices();

assert_eq!(Some((0, 'y')), char_indices.next()); // not (0, 'yฬ†')
assert_eq!(Some((1, '\u{0306}')), char_indices.next());

 // note the 3 here - the previous character took up two bytes
assert_eq!(Some((3, 'e')), char_indices.next());
assert_eq!(Some((4, 's')), char_indices.next());

assert_eq!(None, char_indices.next());

In my code, I try to find the first char that differ between two string and print a part of the string to see the difference [some char before if possible and some char after if possible].
But how to get the entire char ?
I want to get the entire char 'yฬ†', for exemple knowing just its index...

7 posts - 5 participants

Read full topic

๐Ÿท๏ธ Rust_feed