You've got a few problems:
You've put the &mut
in the wrong place; &mut
is part of the type, not the argument (unless you're destructuring the argument, which you aren't).
You can't call the argument struct
, because that's a keyword.
You can't assign to a mutable reference with straight assignment.
So, with those in mind, here's a working solution:
#[derive(Debug)]
struct MyStruct {
tuple: (i32, i32),
}
fn function(s: &mut MyStruct) {
let (ref mut val1, ref mut val2) = s.tuple;
*val1 = 1;
*val2 = 2;
}
fn main() {
let mut s = MyStruct { tuple: (0, 0) };
function(&mut s);
println!("{:?}", s);
}
The key here is that ref
in a pattern binds by-reference; combining that with mut
gives you a mutable reference. Specifically, it gives you a pair of &mut i32
s. Since these are references, you have to de-reference them in order to assign through them (otherwise, you'd be trying to re-assign the reference itself).