Why are explicit lifetimes needed in Rust?

前端 未结 10 1105
别那么骄傲
别那么骄傲 2020-11-22 16:08

I was reading the lifetimes chapter of the Rust book, and I came across this example for a named/explicit lifetime:

struct Foo<\'a> {
    x: &\'a i         


        
10条回答
  •  忘了有多久
    2020-11-22 16:57

    The case from the book is very simple by design. The topic of lifetimes is deemed complex.

    The compiler cannot easily infer the lifetime in a function with multiple arguments.

    Also, my own optional crate has an OptionBool type with an as_slice method whose signature actually is:

    fn as_slice(&self) -> &'static [bool] { ... }
    

    There is absolutely no way the compiler could have figured that one out.

提交回复
热议问题