Given a value of type Vec<&'static str>
, I can freely convert that to Vec<&'r str>
, as 'r
is a subregion of 'static
. That seems to work for most types, e.g. Vec
, pairs etc. However, it doesn't work for types like Cell
or RefCell
. Concretely, down_vec
compiles, but down_cell
doesn't:
use std::cell::Cell;
fn down_vec<'p, 'r>(x: &'p Vec<&'static str>) -> &'p Vec<&'r str> {
x
}
fn down_cell<'p, 'r>(x: &'p Cell<&'static str>) -> &'p Cell<&'r str> {
x
}
Giving the error:
error[E0308]: mismatched types
--> src/lib.rs:9:5
|
9 | x
| ^ lifetime mismatch
|
= note: expected reference `&'p std::cell::Cell<&'r str>`
found reference `&'p std::cell::Cell<&'static str>`
note: the lifetime `'r` as defined on the function body at 8:18...
--> src/lib.rs:8:18
|
8 | fn down_cell<'p, 'r>(x: &'p Cell<&'static str>) -> &'p Cell<&'r str> {
| ^^
= note: ...does not necessarily outlive the static lifetime
Why does this not work for Cell
? How does the compiler track that it doesn't work? Is there an alternative that can make it work?