I'd like to edit the content inside an Option<T>
without consuming it. The code below seems one way, but I think the use of get_or_insert()
is somewhat unnecessary and redundant.
#[derive(Debug)]
struct S {
state: String,
}
fn main() {
let mut x = Some(S {
state: String::from("Some is definitely set"),
});
let y: &mut S = x.get_or_insert(S {
state: String::from("Just hack. This is definitely not going to be inserted"),
});
y.state = String::from("Check out x");
println!("{:#?}", x);
}
Is there an alternative to get_or_insert()
to pull out reference of Option<T>
without consuming it?