In Rust programming language, can there be a case where 'static
is placed here:
struct Abc <'static> {
...
In Rust programming language, can there be a case where 'static
is placed here:
struct Abc <'static> {
...
This is a bit like asking if can you specify i32
as a type parameter in a struct declaration:
struct Abc<i32> {}
It doesn't make sense[1].
A type parameter lets fields of a struct be generic:
struct Abc<T> {
foo: T, // a user of this struct can choose the type of T
}
In the same way, a lifetime parameter allows the lifetime of a reference to be generic:
struct Abc<'a> {
foo: &'a str, // a user of this struct can choose the lifetime bound
}
If you want the lifetime to be always static, then just specify that, rather than making it generic:
struct Abc {
foo: &'static str, // this must always be static
}
[1] Actually it declares a type parameter, which just happens to have the same name as the i32
type—but that is unlikely to be what a person who tried writing something like this would have intended.
No.
Every type has an implicit 'static
lifetime if no generic lifetime is specified. Lifetimes in the declaration of a structure as in
struct Abc<'here, 'and, 'there>;
are meant to specify that the structure contains shorter lifetimes and give them a name. 'static
being a special lifetime doesn't need to be specified here or to have a local name.
This doesn't mean however that those lifetimes can't be 'static
for a particular instance of the structure.