简体   繁体   English

如何修复Clippy的needless_range_loop用于在具有偏移的切片之间复制的循环?

[英]How do I fix Clippy's needless_range_loop for loops that copy between slices with an offset?

When running cargo clippy , it complains about code like this: 当运行cargo clippy ,它会抱怨如下代码:

pub fn from_bytes(data: [u8; 72]) -> Stuff {
    let mut ts = [0u8; 8];
    let mut cs = [0u8; 64];

    for b in 0..8 {
        ts[b] = data[b];
    }

    for bb in 0..64 {
        cs[bb] = data[bb + 8];
    }
}

with

warning: the loop variable `bb` is used to index `cs`
  --> src/main.rs:9:5
   |
9  | /     for bb in 0..64 {
10 | |         cs[bb] = data[bb + 8];
11 | |     }
   | |_____^
   |
   = note: #[warn(needless_range_loop)] on by default
   = help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#needless_range_loop
help: consider using an iterator
   |     for (bb, <item>) in cs.iter().enumerate().take(64) {

I can not wrap my head around this information . 我无法为这些信息打招呼 How can I change to the suggested method? 如何更改为建议的方法? I don't get how something like 我不明白

for (bb, <item>) in cs.iter().enumerate().take(64)

can be applied to my use case. 可以应用于我的用例。

Use clone_from_slice 使用clone_from_slice

ts.clone_from_slice(&data[..8]);
cs.clone_from_slice(&data[8..]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM