为什么我收到错误:捕获的变量无法转义`FnMut` 闭包体? [英] why I get error: captured variable cannot escape `FnMut` closure body?

查看:61
本文介绍了为什么我收到错误:捕获的变量无法转义`FnMut` 闭包体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写代码,为我的图像中的三个矩形区域应用蒙版.目标是制作一个包含这些区域的矢量,以便我以后可以模糊它们.

I was trying to write code that applies a mask for three rectangulars regions within my image. The goal is to make a vector that contains these regions so that I can blur them later.

这是我的代码:

fn main() {
    let img = image::open(
        "C:/Users/hp/Desktop/Multiprocessor real-time scheduling/Project2/data/aLIEz.jpg",
    )
    .unwrap();
    let mut gray_image = img.to_luma8();

    struct Rectangles {
        line1: Vec<(u32, u32, u32, u32)>,
        line2: Vec<(u32, u32, u32, u32)>,
        line3: Vec<(u32, u32, u32, u32)>,
    }

    let rectangles1 = Rectangles {
        line1: vec![(250, 325, 350, 415)],
        line2: vec![(225, 500, 350, 615)],
        line3: vec![(50, 825, 185, 980)],
    };
    let img_parts: Vec<_> = rectangles1
        .line1
        .iter()
        .map(|(start_i, start_j, stop_i, stop_j)| {
            gray_image.sub_image(*start_i, *start_j, stop_i - start_i, stop_j - start_j)
        })
        .collect();

    gray_image.save("C:/Users/hp/Desktop/Multiprocessor real-time scheduling/Project2/data_output/test12.png").unwrap();
}

请找出以下错误:

    error: captured variable cannot escape `FnMut` closure body
  --> src\main.rs:45:47
   |
17 |     let mut gray_image = img.to_luma8();
   |         -------------- variable defined here
...
45 |     .map(|(start_i, start_j, stop_i, stop_j)| gray_image.sub_image(*start_i, *start_j, stop_i-start_i, stop_j-start_j)).collect();
   |                                             - ----------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |                                             | |
   |                                             | returns a reference to a captured variable which escapes the closure body
   |                                             | variable captured here
   |                                             inferred to be a `FnMut` closure
   |
   = note: `FnMut` closures only have access to their captured variables while they are executing...
   = note: ...therefore, they cannot allow references to captured variables to escape

推荐答案

sub_image 返回 可变视图进入图像.由于借用检查器冲突,您显然不能在同一图像中拥有多个可变视图.所以,使用 GenericImageView::view 代替不可变视图:

sub_image returns a mutable view into the image. You obviously cannot have multiple mutable views into the same image due to borrow checker conflicts. So, use GenericImageView::view for an immutable view instead:

// get the trait into scope
use image::GenericImageView;

fn main() {
    let img = image::open(
        "C:/Users/hp/Desktop/Multiprocessor real-time scheduling/Project2/data/aLIEz.jpg",
    )
    .unwrap();
    let mut gray_image = img.to_luma8();

    struct Rectangles {
        line1: Vec<(u32, u32, u32, u32)>,
        line2: Vec<(u32, u32, u32, u32)>,
        line3: Vec<(u32, u32, u32, u32)>,
    }

    let rectangles1 = Rectangles {
        line1: vec![(250, 325, 350, 415)],
        line2: vec![(225, 500, 350, 615)],
        line3: vec![(50, 825, 185, 980)],
    };
    let img_parts: Vec<_> = rectangles1
        .line1
        .iter()
        .map(|(start_i, start_j, stop_i, stop_j)| {
            // use view instead of sub_image here
            gray_image.view(*start_i, *start_j, stop_i - start_i, stop_j - start_j)
        })
        .collect();

    gray_image.save("C:/Users/hp/Desktop/Multiprocessor real-time scheduling/Project2/data_output/test12.png").unwrap();
}

这篇关于为什么我收到错误:捕获的变量无法转义`FnMut` 闭包体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆