We assume there is an external trait
trait ParseStr<'a>{
fn parse_str(input:&'a str)->Self;
}
Now I want to define a trait like this, so that I could implement join_and_parse
which is a function that could join a series of string slices and parse their combination, surely it was just an example.
trait MyParseStr:ParseStr<'_>{}
fn join_and_parse<T:MyParseStr>(arr:&[&str])->T{
let s=arr.join("");
T::parse_str(&s)
}
But the compliler disagreed with me to do so.
error[E0106]: missing lifetime specifier
--> src/main.rs:7:31
|
7 | trait MyParseStr:ParseStr<'_>{}
| ^^ expected named lifetime parameter
|
help: consider introducing a named lifetime parameter
|
7 | trait MyParseStr<'a>:ParseStr<'a>{}
| ++++ ~~
But obviously, if I modified my code as the compiler suggested, the reference of s
in the function will be considered to be brought out of the function, so it's not the correct workaround. How can I solve this problem or how to implement this function?