You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rust迭代器未遍历所有元素问题排查(Cryptopals挑战)

问题:Cryptopals第1组第5题加密函数迭代器遍历异常

我正在完成Cryptopals第1组第5题挑战,实现了如下Rust加密函数:

fn encrypt(message: &str, key: &str) -> String {
    let mut encrypted_message: Vec<u8> = message.as_bytes().to_vec();
    println!("{:?}", encrypted_message);
    for (index, ch) in key.char_indices() {
        encrypted_message
            .iter_mut()
            //.inspect(|byte| {print!("{}, ", *byte);})
            .skip(index)
            // .inspect(|byte| {print!("{}, ", *byte);})
            .step_by(key.len())
            .inspect(|byte| {print!("{}, ", *byte);})
            .for_each(|byte| *byte = *byte^(ch as u8));
        println!("");
    }
    println!("{:?}", encrypted_message);
    let mut encrypted_string = String::new();
    encrypted_message.iter().for_each(|num| encrypted_string+=&format!("{:02x}", *num)[..]);
    encrypted_string
}

函数中for循环内的迭代器未遍历所有元素,从测试输出可见,处理后的encrypted_message长度不符合预期——我预期它不会变短,但实际情况导致测试断言失败。

内容的提问来源于stack exchange,提问作者Lamri Yacine

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 02:24:54