Creating a sliding window iterator of slices of chars from a String

后端 未结 3 1206
失恋的感觉
失恋的感觉 2021-01-17 23:57

I am looking for the best way to go from String to Windows using the windows function provided for slices.

I understa

3条回答
  •  逝去的感伤
    2021-01-18 00:43

    You can use itertools to walk over windows of any iterator, up to a width of 4:

    extern crate itertools; // 0.7.8
    
    use itertools::Itertools;
    
    fn main() {
        let input = "日本語";
    
        for (a, b) in input.chars().tuple_windows() {
            println!("{}, {}", a, b);
        }
    }
    

    See also:

    • Are there equivalents to slice::chunks/windows for iterators to loop over pairs, triplets etc?

提交回复
热议问题