0

In Rust programming language, can there be a case where 'static is placed here:

struct Abc <'static> {
  ...
croraf
  • 4,332
  • 2
  • 31
  • 50
  • What indicates it might? Have you tried it? – underscore_d Jan 16 '20 at 16:23
  • 1
    I don't think so. The lifetime there is to have a lifetime available sort of as a variable that you can attach to inner fields. But you could do `struct Abc<'a>` and initialize it statically. – Brandon Dyer Jan 16 '20 at 16:24
  • @underscore_d I tried. I didn't manage to do it which doesnt mean it cannot be placed in certain circumstances. – croraf Jan 16 '20 at 16:26
  • "_I didn't manage to do it_" Why not? What error did the compiler report? – underscore_d Jan 16 '20 at 16:28
  • @underscore_d "^^^^^^^ 'static is a reserved lifetime name". But even if you put <'a> without using it is a compiler error, saying it is unused though. – croraf Jan 16 '20 at 16:32
  • What should this mean? You can already put `'static` inside non-generic structs, and you can parametrize generic structs with `'static` explicitly. How would this be different from either of those? – trent Jan 16 '20 at 16:44
  • The motivation for the question comes from `impl Abc<'static> {` which I think is valid. – croraf Jan 16 '20 at 18:30
  • I guess I don't really understand the question. At first I thought you had some idea what `struct Abc<'static> {` would mean if it were allowed, and were asking about that. But it seems you're just asking if the syntax is allowed -- which you answered for yourself: no, `'static` is a reserved lifetime name, you can't use it as the name of a lifetime parameter. So what *is* the question? – trent Jan 16 '20 at 21:11
  • 1
    Exactly that is my original question and the answer which is satisfying. I didn't answer myself. I couldn't find a construct that would allow `struct Abc<'static>{`, but I didn't prove there cannot be any such. – croraf Jan 16 '20 at 21:16
  • Does one of the existing posts satisfy your question, or are you hoping for something else? – trent Jan 17 '20 at 16:33
  • Both satisfy. I will select one as accepted – croraf Jan 17 '20 at 17:00

2 Answers2

4

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.

Peter Hall
  • 53,120
  • 14
  • 139
  • 204
2

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.

trent
  • 25,033
  • 7
  • 51
  • 90
mcarton
  • 27,633
  • 5
  • 85
  • 95
  • `Everything has an implicit 'static lifetime by default if no other lifetime is specified.` That would imply that `&self` in `fn anymethod(&self, ...) -> ... { ... }` has `'static` lifetime, which is simply not true... Lifetime is inferred from context. Rust doesn't assume that it is just `'static`. When it cannot infer, in cases such as struct declarations or specific function declarations, then you have to add it yourself. –  Jan 16 '20 at 19:20
  • 1
    @Sahsahae mcarton is correct: if a concrete type has no lifetime parameters, the type is `'static`. This is not the same as saying that *references* to the type must be `'static`. `String`, for example, is a type with no lifetime parameters, and `String: 'static`, even though `&String` need not be. – trent Jan 16 '20 at 21:04
  • Read, for example, the answers to [The compiler suggests I add a 'static lifetime because the parameter type may not live long enough, but I don't think that's what I want](https://stackoverflow.com/q/40053550/3650362). – trent Jan 16 '20 at 21:06
  • I think the way @trentcl rephrased it is less ambiguous. Thanks @trentcl! – mcarton Jan 16 '20 at 22:45