Instantiate vectors in rust

Hello,

In my code, I want to instantiate this structure:

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

So, I have write the code below:

let rectangles1 = Rectangles {
        line1: vec![250, 325, 350, 415],
        line2: vec![225, 500, 350, 615],
        line3: vec![50, 825, 185, 980],
    };

In my powershell I have this error for all lines :
error[E0308]: mismatched types
--> src\main.rs:28:21
|
28 | line1: vec![250, 325, 350, 415],
| ^^^ expected tuple, found integer
|
= note: expected tuple (u32, u32, u32, u32)
found type {integer}

Please I want suggestions for that.

Hello!
Your code here defines the lines as vector of tuples which contains four u32.

But when you create the Rectangles in your code

You just give the lines in the Rectangles a vector of four u32.
So consider adding parentheses to form a tuple when you create the Rectangles:

let rectangles1 = Rectangles {
        line1: vec![(250, 325, 350, 415)],
        line2: vec![(225, 500, 350, 615)],
        line3: vec![(50, 825, 185, 980)],
};
1 Like

Or you should use Vec<u32> or [u32; 4]

3 Likes

Yes thanks, It works but I have now a new error in my code:
That's my code :

 fn main() {
        let mut img = image::open("C:/Users/hp/Desktop/Multiprocessor real-time scheduling/Project2/data/aLIEz.jpg").unwrap();
        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)| img.sub_image(*start_i, *start_j, stop_i-start_i, stop_j-start_j)).collect();
        
    	img.save("C:/Users/hp/Desktop/Multiprocessor real-time scheduling/Project2/data_output/test12.png").unwrap();

    }

Please find the new error below :

error: captured variable cannot escape FnMut closure body
--> src\main.rs:45:47
|
16 | let mut img = image::open("C:/Users/hp/Desktop/Multiprocessor real-time scheduling/Project2/data/aLIEz.jpg").unwrap();
| ------- variable defined here
...
45 | .map(|(start_i, start_j, stop_i, stop_j)| img.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

Please put your error output in code blocks too, so they are easier to read.

The sub_image method takes a mutable reference and returns a SubImage with a lifetime (reference) tied to your img. In this scenario -- generating a reference from a &mut -- the generated references maintains the exclusive access that the &mut had. You can only have one at a time, just like you can only have one &mut at a time.

Example on the playground.

So you can't have a collection of sub-images of the same image, or more than one sub-image at a time generally (at least, not if they're created via the sub_image method).

1 Like

I want to apply this filter to a gray image:

The filter is in the form of a rectangle inside the image:

<start_i> <start_j> <stop_i> <stop_j> :

250 325 350 415 
225 500 350 615
50 825 185 980

In a nutshell, I have as input a gray image and a filter(region). as output I want to have the reference of the image region. --> So that later, I will be able to perform operations(blurring people face in the image) just on this region of the image.

I understand very well your explanation. But, I can't find other ways to do my task: Applying box filter to some regions in my image

With the methods provided, you can have multiple read-only sub-images via the view method, but only one mutable sub-image at a time. (What would happen if your sub-images overlapped?)

One way to approach this would be to carry around the parameters needed to create the mutable sub-image in their own struct, and then serially create and modify each sub-image later on.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.