趣味编程:静夜思(Rust版)

extern crate itertools;
use itertools::Itertools;

fn main() {
    let text = "床前明月光疑是地上霜举头望明月低头思故乡";
    let offset = 5;
    let v = text.chars().enumerate().sorted_by_key(|x| x.0 % offset).collect_vec();
    for (_, group) in &v.into_iter().group_by(|x| x.0 % offset) {
        let s: String = group.map(|x| x.1).collect_vec().into_iter().rev().intersperse(‘|‘).collect();
        println!("{}", s);
    }
}

/*
低|举|疑|床
头|头|是|前
思|望|地|明
故|明|上|月
乡|月|霜|光
*/

相关推荐