Lifetimes issues while implementing `FromStr` for my struct

The way the FromStr trait is defined, you cannot produce a result which borrows from the input, because FromStr::from_str() always uses an input lifetime unrelated to Self. Your choices are:

  • Don't make Cedict borrow the input string, but own each piece (use String instead of &'a str). This also means that the Cedict is allowed to outlive the input, which might be desirable — right now, you're going to have to use the Cedict only within the stack frame that created it.
  • Don't use FromStr; just write your own function in impl Cedict {}. Then you're free to design the signature as you need it. (Or if you really want a trait, implement TryFrom<&'a str>.)

Another recent thread on the same problem:

2 Likes