可变借款中的不可变借款

时间:2018-10-17 09:23:03

标签: image rust mutable

我试图在循环访问其他像素数据的同时访问这些像素。

extern crate image; // 0.19.0

const IMG_H: u32 = 10;
const IMG_W: u32 = 10;

fn foo(imgbuf: &mut image::ImageBuffer<image::Luma<u8>, Vec<u8>>, points: &Vec<u16>) {
    for (_x, _y, pixel) in imgbuf.enumerate_pixels_mut() {
        for p in points {
            let x2 = *p as u32 / IMG_H;
            let y2 = *p as u32 % IMG_W;
            let i = imgbuf.get_pixel(x2, y2)[0];
            *pixel = image::Luma([i]);
        }
    }
}

我收到此错误:

error[E0502]: cannot borrow `*imgbuf` as immutable because it is also borrowed as mutable
  --> src/lib.rs:11:21
   |
7  |     for (_x, _y, pixel) in imgbuf.enumerate_pixels_mut() {
   |                            ------                      - mutable borrow ends here
   |                            |
   |                            mutable borrow occurs here
...
11 |             let i = imgbuf.get_pixel(x2, y2)[0];
   |                     ^^^^^^ immutable borrow occurs here

除了复制图像缓冲区以访问某些值之外,还有其他解决方案吗?

0 个答案:

没有答案