I have a struct that owns a HashMap<String, String>
,
struct Test {
data: HashMap<String, String>,
}
I am trying to implement the Index
trait for this type to map to the Index
implementation of the hashmap (there's other logic involved so I cannot expose the hashmap).
This works if I am just getting a reference to the value in the hashmap:
impl<'b> Index<&'b str> for Test {
type Output = String;
fn index(&self, k: &'b str) -> &String {
self.data.get(k).unwrap()
}
}
However, I want to get &Option<&String>
out of it, like data.get()
. So I tried this:
impl<'b, 'a> Index<&'b str> for Test {
type Output = Option<&'a String>;
fn index(&'a self, k: &'b str) -> &Option<&'a String> {
&self.data.get(k)
}
}
This results in:
error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
--> <anon>:8:10
|
8 | impl<'b, 'a> Index<&'b str> for Test {
| ^^ unconstrained lifetime parameter
I understand the "unconstrained lifetime parameter
in 'a
". Now 'a
is the lifetime of Test
itself, so I want (I think) where 'Self: 'a
(so self
lives at least as long as 'a
) . I cannot seem to figure this out for Index
impl? I tried some things with adding PhantomData
to my Test
. But I am not getting anywhere. Any suggestions?
index()
will always return a reference and you can't return a reference to the function localOption<T>
, because it doesn't live long enough. Apart from that, I think (just a wild guess) that the lifetime issue is actually the same as with streaming iterators. If that's the case, Rust's type system is not powerful enough to express this yet. But anyway, you don't want to return anOption<T>
viaindex()
, as it's expected to panic on an invalid index. Just write aget()
method, like the hashmap does. I hope that maybe helped. – Lukas KalbertodtIndex
should panic, soget()
is the better choice. – divbyzero