想遍历一个字符串,半天没有找到能 work 的
name="hello world"
for (i:=name.len();i>0;i++){
// rust 中没有这种比较简单的写法, 只能 loop ?
}
loop{
// DOOOOOOOO
}
1
BigDataIsDead Nov 5, 2020
let mut a = String::from("food");
for i in 0..a.len() { print!("{} ", a.as_bytes()[i] as char); } |
2
BigDataIsDead Nov 5, 2020
优雅的写法
```rust for i in a.chars() { print!("{} ", i ); } ``` |
3
lostpg Nov 5, 2020
只是字幕数字的话用 as_bytes,遍历 unicode code points 的话,chars() 和 char_indices() 方法。如果有的字符是多个 code points 组成的话,用 https://crates.io/crates/unicode-segmentation 这个 crate 。
|