Parse a string into another type.
The parse method on str attempts to convert the string into the specified type by using the FromStr trait. It returns a Result that is Ok with the parsed value on success, or Err with a parse error on failure.
The target type is usually inferred from context or specified with the turbofish syntax (::<>). Common target types include numeric types, IpAddr, bool, and any custom type implementing FromStr.
parse is the idiomatic way to convert strings to numbers in Rust. It handles leading and trailing whitespace only if the target type's FromStr implementation does so.
let n: i32 = "42".parse().unwrap();
let pi: f64 = "3.14".parse().unwrap();
let bad: Result<i32, _> = "abc".parse();
assert!(bad.is_err());