У меня есть черта, с которой я хочу иметь связанную константу, где константа будет срезом типа, который реализует черту. Примерно так:
trait A: Sized {
const VALUES: &'static [Self];
}
#[derive(Debug)]
struct B {
b: u8,
}
impl A for B {
const VALUES: &'static [B] = &[B { b: 1 }];
}
#[derive(Debug)]
struct C {
c: usize,
}
impl A for C {
const VALUES: &'static [C] = &[C { c: 4 }];
}
fn main() {
println!("{:?}", B::VALUES);
println!("{:?}", C::VALUES);
}
Это не скомпилируется с ошибкой:
error[E0310]: the parameter type `Self` may not live long enough
--> src/main.rs:2:5
|
2 | const VALUES: &'static [Self];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider adding an explicit lifetime bound `Self: 'static`...
note: ...so that the reference type `&'static [Self]` does not outlive the data it points at
Как эта граница времени жизни выражается для связанной константы? Если это уместно, я использую последнюю rustc
стабильную (1.42.0
) и версию 2018 года.