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?
ogeon
April 23, 2015, 1:24pm
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.
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
ogeon
April 23, 2015, 1:45pm
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>
.
1 Like
Many thanks Ogeon, it worked! now I understand better.
1 Like