1

How to rewrite what follows to have it all in one line, in function signature:

fn process(tup: &mut (u32,u32,&mut image::Luma<u8>)) { let &mut (x,y, _) = tup; let ref mut pixel = *tup.2; 

I got as far as:

fn process(&mut (x,y, ref mut pixel): &mut (u32,u32,&mut image::Luma<u8>)) { 

but that's not exact equivalent because I no longer can do:

*pixel = image::Luma([i as u8]); 

inside the function, that I could do when I had temporary tup binding.

Fails with:

src\main.rs:43:14: 43:36 note: expected type `&mut image::Luma<u8>` src\main.rs:43:14: 43:36 note: found type `image::Luma<u8>` 

I also tried:

process(&mut (x, y, pixel): &mut (u32,u32,&mut image::Luma<u8>)) 

but this fails with:

src\main.rs:23:12: 23:29 error: cannot move out of borrowed content [E0507] src\main.rs:23 fn process(&mut (x,y, pixel): &mut (u32,u32,&mut image::Luma<u8>)) { ^~~~~~~~~~~~~~~~~ src\main.rs:23 fn process(&mut (x,y, pixel): &mut (u32,u32,&mut image::Luma<u8>)) { ^~~~~ 

Basically what I need is pattern that can destructure reference to borrowed value from a borrow.

4
  • I'd like to point that in this specific case, you should be able to write process((x, y, pixel): (u32, u32, &mut X)). The outer &mut (on both sides) is useless here. Commented Jun 20, 2016 at 15:44
  • Does not compile without it due to type mismatch: src\main.rs:72:33: 72:34 note: expected type (u32, u32, &mut image::Luma<u8>) src\main.rs:72:33: 72:34 note: found type &mut (u32, u32, &mut _) I also can't deference at the call site because cannot move out of borrowed content. Would work if tuple third element wasn't a borrow. Commented Jun 20, 2016 at 15:58
  • Damned... that a sticky situation you found yourself in! Commented Jun 20, 2016 at 16:50
  • Just tried to use github.com/PistonDevelopers/image with their example 6.2 (drawing Julia fractal) and extract calculation to separate function. ... The more I fall, the more I learn. Anyways, thanks for help! Commented Jun 20, 2016 at 17:14

1 Answer 1

0
fn process(&mut (x,y, &mut ref mut pixel): &mut (u32,u32,&mut image::Luma<u8>)) { 

This pattern &mut (x,y, &mut ref mut pixel) makes the pixel mutable reference to borrowed value.

&mut in &mut ref mut pixel unwraps the value from the borrow before ref mut makes the pixel reference to it.

I found this solution after looking here: http://rustbyexample.com/flow_control/match/destructuring/destructure_pointers.html

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.