How do I fix a missing lifetime specifier?

后端 未结 2 640
我寻月下人不归
我寻月下人不归 2021-01-05 15:32

I have a very simple method. The first argument takes in vector components (\"A\", 5, 0) and I will compare this to every element of another vector to see if they have the s

2条回答
  •  旧时难觅i
    2021-01-05 16:11

    So the problem comes from the fact that vector has two inferred lifetimes, one for vector itself (the &Vec part) and one for the &str inside the vector. You also have an inferred lifetime on x, but that really inconsequential.

    To fix it, just specify that the returned &str lives as long as the &str in the vector:

    fn is_same_space<'a>(                        // Must declare the lifetime here
        x: &str,                                 // This borrow doesn't have to be related (x isn't even used)
        y1: i32,                                 // Not borrowed
        p: i32,                                  // Not borrowed or used
        vector: &'a Vec<(&'a str, i32, i32)>     // Vector and some of its data are borrowed here
    ) -> &'a str {                               // This tells rustc how long the return value should live
        ...
    }
    

提交回复
热议问题