Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

rust - Cannot infer appropriate lifetime for autoref when calling a method from an Iterator implementation

I am trying to implement the Iterator trait for a struct which acts as a borrower of an array of i32 values, but I keep running into the compiler complaining about not being able to infer a lifetime inside the next method.

I am aware of Need help understanding Iterator lifetimes, but since my struct just borrows a slice of the array anyway, I keep the memory of the actual elements separate from my IntegerArrayBag.

#[derive(Debug)]
struct IntegerArrayBag<'a> {
    arr: &'a [i32],
    idx: usize,
}

impl<'a> IntegerArrayBag<'a> {
    fn len(&self) -> usize {
        self.arr.len()
    }

    fn get(&self, idx: usize) -> Option<&i32> {
        if self.arr.len() > idx {
            Some(&self.arr[idx])
        } else {
            None
        }
    }
}

impl<'a> Iterator for IntegerArrayBag<'a> {
    type Item = &'a i32;

    fn next(&mut self) -> Option<&'a i32> {
        let idx = self.idx;
        self.idx += 1;
        self.get(idx)
    }
}

If I try to compile this code, the compiler complains with:

error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
  --> src/main.rs:27:14
   |
27 |         self.get(idx)
   |              ^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 24:5...
  --> src/main.rs:24:5
   |
24 | /     fn next(&mut self) -> Option<&'a i32> {
25 | |         let idx = self.idx;
26 | |         self.idx += 1;
27 | |         self.get(idx)
28 | |     }
   | |_____^
note: ...so that reference does not outlive borrowed content
  --> src/main.rs:27:9
   |
27 |         self.get(idx)
   |         ^^^^
note: but, the lifetime must be valid for the lifetime 'a as defined on the impl at 21:1...
  --> src/main.rs:21:1
   |
21 | / impl<'a> Iterator for IntegerArrayBag<'a> {
22 | |     type Item = &'a i32;
23 | |
24 | |     fn next(&mut self) -> Option<&'a i32> {
...  |
28 | |     }
29 | | }
   | |_^
note: ...so that expression is assignable (expected std::option::Option<&'a i32>, found std::option::Option<&i32>)
  --> src/main.rs:27:9
   |
27 |         self.get(idx)
   |         ^^^^^^^^^^^^^
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to update your get method to return a reference with longer life:

// Use 'a from impl<'a> IntegerArrayBag<'a>
fn get(&self, idx: usize) -> Option<&'a i32> {

and then it will compile.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...