Is there a way to transform the match
statement in the following code to the simplified 2018 edition syntax with automatic borrows?
enum Entry {
Dir { mtime: i64 },
File { mtime: i64, _size: u64 },
}
fn touch(entry: &mut Entry, mtime: i64) {
let x: &mut i64 = match *entry {
Entry::Dir { ref mut mtime } => mtime,
Entry::File { ref mut mtime, .. } => mtime,
};
*x = mtime;
}
fn main() {
let mut entry1 = Entry::Dir { mtime: 0 };
touch(&mut entry1, 1);
let mut entry2 = Entry::File {
mtime: 0,
_size: 1024,
};
touch(&mut entry2, 1);
}
This is what I tried, but the lifetime of the references were limited to the end of the match
statement.
let x: &mut i64 = match entry {
Entry::Dir { mut mtime } => &mut mtime,
Entry::File { mut mtime, .. } => &mut mtime,
};
error[E0597]: `mtime` does not live long enough
--> src/main.rs:8:37
|
7 | let x: &mut i64 = match entry {
| - borrow later stored here
8 | Entry::Dir { mut mtime } => &mut mtime,
| ^^^^^^^^^^ borrowed value does not live long enough
9 | Entry::File { mut mtime, .. } => &mut mtime,
10 | };
| - `mtime` dropped here while still borrowed