Trait FromStr
trait FromStr: Sized
Parse a value from a string
FromStr's from_str method is often used implicitly, through
str's parse method. See parse's documentation for examples.
FromStr does not have a lifetime parameter, and so you can only parse types
that do not contain a lifetime parameter themselves. In other words, you can
parse an i32 with FromStr, but not a &i32. You can parse a struct that
contains an i32, but not one that contains an &i32.
Input format and round-tripping
The input format expected by a type's FromStr implementation depends on the type. Check the
type's documentation for the input formats it knows how to parse. Note that the input format of
a type's FromStr implementation might not necessarily accept the output format of its
Display implementation, and even if it does, the Display implementation may not be lossless
so the round-trip may lose information.
However, if a type has a lossless Display implementation whose output is meant to be
conveniently machine-parseable and not just meant for human consumption, then the type may wish
to accept the same format in FromStr, and document that usage. Having both Display and
FromStr implementations where the result of Display cannot be parsed with FromStr may
surprise users.
Examples
Basic implementation of FromStr on an example Point type:
use FromStr;
;
let expected = Ok;
// Explicit call
assert_eq!;
// Implicit calls, through parse
assert_eq!;
assert_eq!;
// Invalid input string
assert!;
Associated Types
type ErrThe associated error which can be returned from parsing.
Required Methods
fn from_str(s: &str) -> Result<Self, <Self as >::Err>Parses a string
sto return a value of this type.If parsing succeeds, return the value inside
Ok, otherwise when the string is ill-formatted return an error specific to the insideErr. The error type is specific to the implementation of the trait.Examples
Basic usage with
i32, a type that implementsFromStr:use FromStr; let s = "5"; let x = i32from_str.unwrap; assert_eq!;
Implementors
impl FromStr for u64impl FromStr for isizeimpl FromStr for u8impl FromStr for i64impl FromStr for NonZero<i16>impl FromStr for NonZero<i128>impl FromStr for NonZero<u8>impl FromStr for i8impl FromStr for f16impl FromStr for u128impl FromStr for u16impl FromStr for charimpl FromStr for SocketAddrV6impl FromStr for NonZero<i8>impl FromStr for i128impl FromStr for NonZero<usize>impl FromStr for NonZero<u128>impl FromStr for f32impl FromStr for NonZero<u64>impl FromStr for NonZero<u32>impl FromStr for NonZero<u16>impl FromStr for i16impl FromStr for NonZero<i64>impl FromStr for SocketAddrimpl FromStr for boolimpl FromStr for u32impl FromStr for f64impl FromStr for Ipv4Addrimpl FromStr for usizeimpl FromStr for SocketAddrV4impl FromStr for Ipv6Addrimpl FromStr for i32impl FromStr for IpAddrimpl FromStr for NonZero<i32>impl FromStr for NonZero<isize>