I have a reference to a struct.
I need a reference to a struct. Sometimes it's the same, but sometimes it's a cloned then modified one.
Right now, I do
if condition {
let mut copy = original.clone();
copy.select();
do_big_thing(©);
} else {
do_big_thing(original);
}
But do_big_thing
takes other arguments, I don't like to duplicate this line.
Is there a way to
- first build the reference, which would either be the original one or a reference to a local modified clone
- then use it in
do_big_thing
(it doesn't have to live longer)
?
Of course this doesn't work because copy
doesn't live long enough:
let mut reference = if condition {
let mut copy = original.clone();
copy.select();
©
} else {
original
};
do_big_thing(reference);
As this question is about finding a cleaner and less cluttered way to write the same thing (either use the original reference or clone), I can't accept a solution which would add overhead at runtime or some unsafe
.