I'd like to change my_id
, only if present, to another value:
fn read(id: &mut i32) {
*id = 42;
}
struct S {
my_id: Option<i32>,
}
impl S {
fn run(&mut self) {
match self.my_id {
Some(mut id) => read(&mut id),
_ => (),
}
}
}
fn main() {
let mut s = S { my_id: 0.into() };
s.run();
println!("{:?}", s.my_id);
}
This code prints Some(0)
, which means the substitution failed, but I don't understand why. Am I losing the mutability because of pattern matching?