How to destroy a link to value instead of a link to borrow?

How to rewrite what follows so that everything is on the same line, in the function signature:

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

Reached:

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

but this is not the exact equivalent, because I can no longer do:

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

inside a function that I could do when I had a time reference tup.

Failure:

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:

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, I need a template that can break the link to borrowed money from borrowing.

+1
source share
1 answer
fn process(&mut (x,y, &mut ref mut pixel): &mut (u32,u32,&mut image::Luma<u8>)) {

This template &mut (x,y, &mut ref mut pixel)makes the replaced link a pixelborrowed value.

&mutin &mut ref mut pixelexpands the value from the loan before ref mutmaking a reference pixelto it.

, : http://rustbyexample.com/flow_control/match/destructuring/destructure_pointers.html

0

All Articles