I'm trying to figure out if libraries should implement both FromStr
and TryFrom<&str>
for types that can be created from strings. For now I think they should and don't think there is a semantic difference between them. However there's a blogpost which claims there is and I found people both agreeing and disagreeing. What does wider Rust community think?
My arguments:
-
FromStr
was created beforeTryFrom
, which is an indication thatTryFrom
is superior because it's more general and flexible - I remember discussions about
impl<'a, T: FromStr> TryFrom<&'a str> for T
and deprecatingFromStr
but it didn't happen because it would conflict withimpl<U, T: Into<U>> TryFrom<T> for U
which was more important. - Including also
TryFrom<String>
can avoid allocations in some cases and it makes sense to also haveTryFrom<&str>
-
TryFrom<&str>
allows borrowing the input which may be useful for types containingCow<'_, str>
-
std
documentation doesn't say anything about it but it does mention semantic differences of other traits that are similar to each-other (e.g.AsRef
vsBorrow
,io::Write
vsfmt::Write
); could be just documentation issue though