Here is a code snippet illustrating a lifetimes issue I ran into:
use std::collections::HashSet;
pub trait Store {
fn put<'a, 'b: 'a>(&mut self, val: &'b str);
}
struct Hstore<'a> {
hset: HashSet<&'a str>,
}
impl<'a> Store for Hstore<'a> {
fn put<'b, 'c: 'b>(&mut self, val: &'c str) {
self.hset.insert(val);
}
}
My intention is to store referenced values passed by the trait in the struct as long as their lifetimes outlives that of the struct.
A get a compilation error:
error: lifetime may not live long enough
--> src/lib.rs:14:9
|
12 | impl<'a> Store for Hstore<'a> {
| -- lifetime `'a` defined here
13 | fn put<'b, 'c: 'b>(&mut self, val: &'c str) {
| -- lifetime `'c` defined here
14 | self.hset.insert(val);
| ^^^^^^^^^^^^^^^^^^^^^ argument requires that `'c` must outlive `'a`
|
= help: consider adding the following bound: `'c: 'a`
How do I add this 'c: 'a
bound? I tried several approaches which did not work.
Also why can't the compile infer the life 'b
in the implementation has the same lifetime validity as 'a
.