Erro: found concrete lifetime


#1

Hello guys, I am trying but, I am not being able to understand what does this error below mean:

src/lib/core/doc.rs:50:5: 52:6 error: method index has an incompatible type for trait: expected bound lifetime parameter 'a, found concrete lifetime [E0053]
src/lib/core/doc.rs:50 fn index(&'a self, _index: &'a str) -> &'a Row {
src/lib/core/doc.rs:51 self
src/lib/core/doc.rs:52 }

Can someone help me with this?


#2

Looks like the definition is displayed differently in the documentation for std and core, where the trait comes from. This shows that the signature for index is fn index<'a>(&'a self, index: Idx) -> &'a Self::Output. Note the index<'a> part.


#3

Ogeon,

Thank you for your response, but for example I have this struct:

#[derive(Debug)]
pub struct Row<'a> {
    stm         : String,
    field       : &'a str
}

How could I implement Index correctly? If I try to do this:

impl Index<String> for Row {
    type Output = Row;

    fn index<'b>(&'b self, _index: String) -> &'b Row {
        self
    }
}

I get this error:

error: wrong number of lifetime parameters: expected 1, found 0


#4

You have to define a separate lifetime parameter for the struct itself:

impl<'a> Index<String> for Row<'a> {
    type Output = Row<'a>;

    fn index<'b>(&'b self, _index: String) -> &'b Row<'a> {
        self
    }
}

'b is the lifetime of the borrow of self in index and 'a is the lifetime of the content in Row<'a>.


#5

Many thanks Ogeon, it worked! now I understand better.