更新时间:2023-11-17 17:51:34
我需要通过字符扫描来迭代它.
I need to iterate by characters scanning for it.
.chars()
方法返回字符串中字符的迭代器.例如
The .chars()
method returns an iterator over characters in a string. e.g.
for c in my_str.chars() {
// do something with `c`
}
for (i, c) in my_str.chars().enumerate() {
// do something with character `c` and index `i`
}
如果你对每个char的字节偏移量感兴趣,可以使用char_indices
.
If you are interested in the byte offsets of each char, you can use char_indices
.
查看 .peekable()
,并使用 peek()
进行查看.之所以这样包装,是因为它支持 UTF-8 代码点,而不是简单的字符向量.
Look into .peekable()
, and use peek()
for looking ahead. It's wrapped like this because it supports UTF-8 codepoints instead of being a simple vector of characters.
您也可以创建一个 char
向量并从那里开始处理它,但这需要更多的时间和空间:
You could also create a vector of char
s and work on it from there, but that's more time and space intensive:
let my_chars: Vec<_> = mystr.chars().collect();