error[E0597]: `vec` does not live long enough
--> longenough.rs:14:19
|
14 | let vec_ref = vec.iter_mut().map(|b| &mut **b).collect();
| ^^^ borrowed value does not live long enough
15 | f(vec_ref);
16 | }
| - `vec` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created
However I don't understand what this is complaining about. Would anyone be able to give some hint?
One thing that confuses me is that changing the uses of dyn Tr with almost anything else (like T or i32) seems to cause the error to disappear. Also I can work around this issue by using a newtype wrapper around &'a mut dyn Tr.
Here is the code:
struct T {}
trait Tr {}
impl Tr for T {}
impl Drop for T {
fn drop(&mut self) {
println!("dropping T")
}
}
fn f(_: Vec<&mut dyn Tr>) {}
fn main() {
let mut vec: Vec<Box<dyn Tr>> = vec![Box::new(T {})];
let vec_ref = vec.iter_mut().map(|b| &mut **b).collect();
f(vec_ref);
}
then it seems to work. I've never really understood how lifetime elision works with trait objects but based on trial and error it seems the original f is an elided version of:
Thank you, I didn't realize that there was a hidden lifetime bound on trait object types. The rules in the reference seem to give what you described: Lifetime elision - The Rust Reference
However, I still don't understand why this is an error. The type of f seems to say that the concrete type of the dyn object must outlive the reference, and indeed it does. So what is the problem...?
Also I found something weird: I can make the code compile (without changing the type of f) by changing the definition of vec_ref to: