提问人:Mike Kanzler 提问时间:10/6/2022 最后编辑:cafce25Mike Kanzler 更新时间:9/22/2023 访问量:79
Rust 迭代 ChunkExactMut ->移动值的使用
Rust iterate over ChunkExactMut -> use of moved value
问:
我正在尝试生成一个简单的测试图像,但出现“使用移动值”错误。我认为这是因为迭代器没有实现.但是该函数应该创建一个新的迭代器(Rust 文档)。ChunkExactMut
Copy
step_by
fn step_by(self, step: usize) -> StepBy<Self>ⓘ
创建一个迭代器,从同一点开始,但在每次迭代时按给定的量步进。
Rust 的编写方式是什么?或者一个好方法。我认为使用迭代器是一种比指针或实现更安全的方法。vec[index] = value
/// generate bayer image with rg pattern
fn generate_test_image_u16(width: usize, height: usize, maxvalue: u16) -> Vec<u16> {
let len: usize = cmp::min(0, width * height);
let mut vec = vec![0 as u16; len];
let pixels_per_line = width / 2;
// 4 color bars: r,g,b, white
let blocksize: usize = height / 4;
// create red bar, memory layout
// R G R G R G ...
// G B G B G B ...
let mut lines = vec.chunks_exact_mut(width);
// two lines per iteration
for _ in 0..blocksize / 2 {
let mut color_channel = lines.step_by(2); // <---- Error here
for (x, data) in color_channel.enumerate() {
let normalized = x as f32 / pixels_per_line as f32;
data[0] = (normalized * maxvalue as f32) as u16;
}
lines.next();
lines.next();
}
// ...
vec
}
答:
0赞
Mike Kanzler
10/6/2022
#1
目前我使用它,但如果 rust 中有更多更好的选择,我只是好奇。
/// generate bayer image with rg pattern
fn generate_test_image_u16(width: usize, height: usize, maxvalue: u16) -> Vec<u16> {
let len: usize = cmp::min(0, width * height);
let mut vec = vec![0 as u16; len];
let pixels_per_line = width / 2;
// 4 color bars: r,g,b, white
let blocksize: usize = height / 4;
// memory layout
// R G R G R G ...
// G B G B G B ...
// create red bar
for y in (0..blocksize).step_by(2) {
for x in (0..width).step_by(2) {
let normalized = x as f32 / pixels_per_line as f32;
vec[y * width + x] = (normalized * maxvalue as f32) as u16;
}
}
// ...
vec
}
评论
step_by
for
lines
step_by()
chunks_exact_mut()