Type System 7 @Noratrieb
Warning: this quiz is still "work-in-progress", some questions might not have good explanations (or any at all), formatting/structure/titles/etc are not final, and so on. You might want to return here on a later date.
trait Cat {}
// meow!
impl<T> Cat for T {}
fn main() {
let r = &0;
require_box(Box::new(r));
let local = 0;
let r = &local;
require_box(Box::new(r));
}
// Cats love boxes.
fn require_box(_a: Box<dyn Cat>) {}
Solution
error[E0597]: `local` does not live long enough
--> examples/trait_solver_7.rs:11:13
|
10 | let local = 0;
| ----- binding `local` declared here
11 | let r = &local;
| ^^^^^^ borrowed value does not live long enough
12 | require_box(Box::new(r));
| ----------- cast requires that `local` is borrowed for `'static`
13 | }
| - `local` dropped here while still borrowed
|
= note: due to object lifetime defaults, `Box<dyn Cat>` actually means `Box<(dyn Cat + 'static)>`
For more information about this error, try `rustc --explain E0597`.
error: could not compile `code` (example "trait_solver_7") due to 1 previous error
Box<dyn Cat>
means Box<dyn Cat + 'static>
, and the local variable is not 'static
. &0
does produce a &'static i32
though, because it is implicitly static-promoted.