I have a struct that contains some &str
, and I’m having trouble implementing FromStr
for it, because the lifetime annotations don’t fit the trait. Here’s the reduced case:
https://play.rust-lang.org/?gist=a4d154d672e4edc98e8d7d1ffe45eb43&version=stable
In the example, in main s
is of lifetime 'static
, which is not true in my usecase, but I do not think it matters.
Here’s my problem: From main
's point of view, everything should be ok: s
lives longer than t
, so t
can contain a reference to s
. But I need to convey that in the signature of the function, that the returned struct does not live longer than the reference passed in the function. The signature captures this meaning (putting it in an impl block for the struct makes it work).
But I get the following error message:
error[E0195]: lifetime parameters or bounds on method `from_str` do not match the trait declaration
--> src/main.rs:11:5
|
11 | / fn from_str<'c, 'b:'c>(s: &'b str) -> result::Result<RunConfig<'c>, Self::Err> {
12 | | Ok(RunConfig {name: s})
13 | | }
| |_____^ lifetimes do not match trait
Ok, it’s not exactly the right signature, but I need those lifetimes, otherwise it can’t conceivably work.
What am I to do? I do not want the structure to own the string, but only keep a reference. I could just not implement the trait and implement the function on the structure itself, but that feels wrong to me…
Thanks for any pointers