(Pre-)RFC: Deprecate FromStr in favor of TryFrom<&str>

In the current libstd, .try_into() and .parse() are different for how they should work.

.try_into(), as a fallible version of .into(), mostly transforms the "shape" of the value to fit another type.

.parse() can be considered the reverse of .to_string(), will read the string content to reconstruct a value in the target type.

Additionally, TryFrom must satisfy this relationship, which is not bound for FromStr:

  • T: From<S>T: TryFrom<S, Error=!>

As a concrete example:

use std::convert::TryInto;
use serde_json::{Value, json}; 

fn main() {
    let a: Value = "[1, 2, 3]".try_into().unwrap();
    let b: Value = "[1, 2, 3]".parse().unwrap();
    assert_eq!(dbg!(a), json!("[1, 2, 3]"));
    assert_eq!(dbg!(b), json!( [1, 2, 3] ));
}
15 Likes