How can I force Rust borrow checker to forget about mutable borrow, with unsafe or not? I assumed that even lexical lifetimes should allow reusing mutable reference after a brackets block, but even Non-Lexical-Lifetimes from the fresh Rust release forbids that. With no outcome I tried some variants of unsafe and std::mem::drop.
#[derive(Debug)]
struct Storage {
s: String,
v: Vec<u8>,
}
#[derive(Debug)]
enum Ret<'a> {
S(&'a mut String),
V(&'a mut Storage),
}
fn fake_get_s<'a, 'b:'a>(_unused_storage: &'b mut Storage) -> Option<&'a mut String> {
None
}
fn get_ret<'a> (storage: &'a mut Storage) -> Ret<'a> {
// Borrowed for a limited block, assuming to release..
{
match fake_get_s(storage) {
Some(string_ref) => {
return Ret::S(string_ref);
}
None => (),
}
// ..with no one keeping the borrow..
}
// ..but, surprisingly, I still can't borrow the origin variable
// Ridiculous, right?
Ret::V(storage)
}
fn main() {
let mut storage = Storage{s: "string".to_string(), v: vec![1, 2, 3]};
let res = get_ret(&mut storage);
println!("{:?}", res);
}