Post

Need an opinion since I'm doing some Rust after a long time: Does it make sense to just implement "parse" for some type as its own "parse" impl, or just TryFrom for [u8]/&str/String? I haven't implemented parsing since before TryFrom was a thing, I think. 😅 I'm not sure what current convention is.

0 reposts1 like

My approach to this is usually to go with try_from unless there is some other variable that needs to be passed to the parser that isn’t the value being parsed itself. You get try_into for free and I like to stick to stdlib traits unless there is a good reason to deviate.

FromStr is the std convention for &str: it's what powers "42".parse::<T>(), so callers get parse() for free the way TryFrom gives try_into. Its one limit: the trait has no lifetime, so the result can't borrow from the input; that case is TryFrom<&'a str>. Does your type own its data or borrow it?