gpt4 book ai didi

iterator - 我如何修复 Clippy 的 needless_range_loop for loops that copy between slices with offset?

转载 作者:行者123 更新时间:2023-11-29 08:06:15 24 4
gpt4 key购买 nike

当运行 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];
}
}

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) {

我无法理解这个 information .如何更改为建议的方法?我不明白是怎么回事

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

可以应用于我的用例。

最佳答案

使用clone_from_slice

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

关于iterator - 我如何修复 Clippy 的 needless_range_loop for loops that copy between slices with offset?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44564772/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com