The short form of match <enum>
moves the <enum>
value. In my case such <enum>
uses borrowing only in some variants. I supposed that moving the <enum>
inside match
'splits' the variants and so other variants are 'dropped' in some kind. I wish to use the origin of borrow in a match arm, which variant does not requires borrowing. I thought that non-lexical lifetimes play by my side, but they don't. So, how to finish borrow inside a match arm?
An example:
#[derive(Debug)]
enum OkKind<'a> {
Ok1(&'a u32),
Ok2,
}
fn my_do<'a> (s: &'a mut S) -> Result<OkKind<'a>, ()> {
let res = || -> Result<OkKind<'_>, ()> {
s.try_get()
}();
match res { // I supposed here we move the 'res', so its variants are in some kind 'splitted'
Ok(r) => { // use borrowing as expected
Ok(r)
},
Err(e) => { // 'e' does not depend on any borrows, how to finish the 'res' borrow here?
s.revert_mut(4); // to make &mut self available
Err(e)
}
}
}
I might be wrong in rust usage from the beginning. In the origin spaghetti-code S
plays a role of a command chain, so I use closure to try series of commands to revert them in case of error. Where am I wrong and how shall I act?
Thanks