Why the code can'e be compiled

⚓ Rust    📅 2026-02-19    👤 surdeus    👁️ 6      

surdeus

The code refuses to compile,

    pub fn iter_mut<'a>(mut self: Pin<&mut Self>) -> IterMut<'a, T, N> {
        let this = self.as_mut().project().h;
        let addr = this.addr();

        let x: Option<BorrowedRawMut<'a>> = Self::next_mut(addr, this);
        IterMut {
            next: x, 
            list: self.into_ref().get_ref(),
            addr,
        }
    }

saying:

error: lifetime may not live long enough
   --> src/lib.rs:408:9
    |
403 |       pub fn iter_mut<'a>(mut self: Pin<&mut Self>) -> IterMut<'a, T, N> {
    |                       --                - let's call the lifetime of this reference `'1`
    |                       |
    |                       lifetime `'a` defined here
...
408 | /         IterMut {
409 | |             next: x, 
410 | |             list: self.into_ref().get_ref(),
411 | |             addr,
412 | |         }
    | |_________^ method was supposed to return data with lifetime `'a` but it is returning data with lifetime `'1`

But x is indeed Option<BorrowedRawMut<'a>> , not Option<BorrowedRawMut<'1>>

And if I changed to next: x, to next: todo!(), it compiles, meaning x does be of type Option<BorrowedRawMut<'a>>;

Edit:
My fault... I missed 'a in the signature... mut self: Pin<&mut Self> should be mut self: Pin<&'a mut Self>

2 posts - 2 participants

Read full topic

🏷️ Rust_feed