我正在寻找从String
到Windows<T>
的最佳方法,使用提供给slices的windows
函数。
我知道如何这样使用windows:
fn main() {
let tst = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
let mut windows = tst.windows(3);
// prints ['a', 'b', 'c']
println!("{:?}", windows.next().unwrap());
// prints ['b', 'c', 'd']
println!("{:?}", windows.next().unwrap());
// etc...
}
但是在解决这个问题时,我有点迷茫:
fn main() {
let tst = String::from("abcdefg");
let inter = ? //somehow create slice of character from tst
let mut windows = inter.windows(3);
// prints ['a', 'b', 'c']
println!("{:?}", windows.next().unwrap());
// prints ['b', 'c', 'd']
println!("{:?}", windows.next().unwrap());
// etc...
}
基本上,我正在寻找如何将字符串转换为字符切片,以便我可以使用窗口方法。
char_windows
的游乐场链接,并进行了轻微修改,以展示与Chunks
等效的方法。 - Charles German