Why is the bound `T: 'a` required in order to store a reference `&'a T`?

后端 未结 2 1661
情歌与酒
情歌与酒 2020-12-01 15:53

Given this code:

struct RefWrapper<\'a, T> {
    r: &\'a T,
}

... the compiler complains:

error: the param

2条回答
  •  余生分开走
    2020-12-01 16:31

    Congratulations, you were right! As of Rust 1.31, thanks to RFC 2093, Infer T: 'x outlives requirements on structs, the requirement on the user to type out this restriction has been removed:

    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, to T with lifetime 'x, then we will infer that T: 'x is a requirement

    Basically, there wasn't a case where this wasn't required, so there wasn't much value in forcing the programmer to write it out.

提交回复
热议问题