Why does it works?
struct X<'a, 'b> {
ref1: &'a (&'b i32,),
}
My understanding it should have explicit promise that 'b
lives at least as long as 'a
:
'b : 'a
.
Why compiler does not require it?
Why does it works?
struct X<'a, 'b> {
ref1: &'a (&'b i32,),
}
My understanding it should have explicit promise that 'b
lives at least as long as 'a
:
'b : 'a
.
Why compiler does not require it?
This struct will actually get an implicit 'b: 'a
bound. So the struct definition is equivalent to if you were to explicitly add that bound as in struct X<'a, 'b: 'a> {
.
Those implicit bounds are unfortunately still very much undocumented in the generated rustdoc (even though IMO, it would be a good thing if they were shown there); so it's an implicit property of the struct, perhaps comparable to how implicit/undocumented the variance of a struct is.
You can observe the implications of this implicit lifetime bound e. g. in code like
fn f<'a, 'b>() {
let _: &X<'a, 'b>;
}
which will fail to compile until you change it to
fn f<'a, 'b: 'a>() {
let _: &X<'a, 'b>;
}
Remove the need for explicit
T: 'x
annotations on structs. We will infer their presence based on the fields of the struct. In short, if the struct contains a reference, directly or indirectly, toT
with lifetime'x
, then we will infer thatT: 'x
is a requirement:
struct Foo<'x, T> {
// inferred: `T: 'x`
field: &'x T
}
That from the RFC and your examples explains a lot. Thank you!
Do you know where exactly?
I meant that the generated Rustdoc should IMO probably present the thing as struct X<'a, 'b: 'a>
again, even if the 'b: 'a
was just inferred.
I see. So do i think.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.