pub(crate) trait FromStr: Sized {
type Err;
// Required method
fn from_str(s: &str) -> Result<Self, Self::Err>;
}
Expand description
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
.
§Examples
Basic implementation of FromStr
on an example Point
type:
use std::str::FromStr;
#[derive(Debug, PartialEq)]
struct Point {
x: i32,
y: i32
}
#[derive(Debug, PartialEq, Eq)]
struct ParsePointError;
impl FromStr for Point {
type Err = ParsePointError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (x, y) = s
.strip_prefix('(')
.and_then(|s| s.strip_suffix(')'))
.and_then(|s| s.split_once(','))
.ok_or(ParsePointError)?;
let x_fromstr = x.parse::<i32>().map_err(|_| ParsePointError)?;
let y_fromstr = y.parse::<i32>().map_err(|_| ParsePointError)?;
Ok(Point { x: x_fromstr, y: y_fromstr })
}
}
let expected = Ok(Point { x: 1, y: 2 });
// Explicit call
assert_eq!(Point::from_str("(1,2)"), expected);
// Implicit calls, through parse
assert_eq!("(1,2)".parse(), expected);
assert_eq!("(1,2)".parse::<Point>(), expected);
// Invalid input string
assert!(Point::from_str("(1 2)").is_err());
Required Associated Types§
Required Methods§
1.0.0 · Sourcefn from_str(s: &str) -> Result<Self, Self::Err>
fn from_str(s: &str) -> Result<Self, Self::Err>
Parses a string s
to 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
inside Err
. The error type is specific to the implementation of the trait.
§Examples
Basic usage with i32
, a type that implements FromStr
:
use std::str::FromStr;
let s = "5";
let x = i32::from_str(s).unwrap();
assert_eq!(5, x);
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementors§
1.0.0 · Source§impl FromStr for core::net::socket_addr::SocketAddr
impl FromStr for core::net::socket_addr::SocketAddr
type Err = AddrParseError
Source§impl FromStr for log::LevelFilter
impl FromStr for log::LevelFilter
type Err = ParseLevelError
Source§impl FromStr for BridgeConfig
impl FromStr for BridgeConfig
type Err = BridgeParseError
Source§impl FromStr for BridgeConfigBuilder
BridgeConfigBuilder
parses the same way as BridgeConfig
impl FromStr for BridgeConfigBuilder
BridgeConfigBuilder
parses the same way as BridgeConfig
type Err = BridgeParseError
Source§impl FromStr for AddrPortPattern
impl FromStr for AddrPortPattern
type Err = PolicyError
Source§impl FromStr for PortPolicy
impl FromStr for PortPolicy
type Err = PolicyError
Source§impl FromStr for HsClientNickname
Available on crate feature restricted-discovery
only.
impl FromStr for HsClientNickname
restricted-discovery
only.Source§impl FromStr for IptLocalId
impl FromStr for IptLocalId
type Err = InvalidIptLocalId
§impl FromStr for HsNickname
impl FromStr for HsNickname
type Err = InvalidNickname
Source§impl FromStr for FutureTimestamp
impl FromStr for FutureTimestamp
type Err = ParseError
Source§impl FromStr for ByteString
impl FromStr for ByteString
type Err = Infallible
1.5.0 · Source§impl FromStr for SocketAddrV4
impl FromStr for SocketAddrV4
type Err = AddrParseError
1.5.0 · Source§impl FromStr for SocketAddrV6
impl FromStr for SocketAddrV6
type Err = AddrParseError
Source§impl FromStr for Url
Parse a string as an URL, without a base URL or encoding override.
impl FromStr for Url
Parse a string as an URL, without a base URL or encoding override.
type Err = ParseError
§impl FromStr for HsClientDescEncKey
impl FromStr for HsClientDescEncKey
1.32.0 · Source§impl FromStr for tor_hsservice::internal_prelude::PathBuf
impl FromStr for tor_hsservice::internal_prelude::PathBuf
type Err = Infallible
§impl FromStr for InternalString
impl FromStr for InternalString
type Err = Infallible
§impl FromStr for Num
impl FromStr for Num
type Err = Either<ParseIntError, ParseFloatError>
§impl FromStr for PathBuf
impl FromStr for PathBuf
type Err = Infallible
§impl FromStr for Protocols
A Protocols set can be parsed from a string according to the
format used in Tor consensus documents.
impl FromStr for Protocols
A Protocols set can be parsed from a string according to the format used in Tor consensus documents.
A protocols set is represented by a space-separated list of
entries. Each entry is of the form Name=Versions
, where Name
is the name of a protocol, and Versions
is a comma-separated
list of version numbers and version ranges. Each version range is
a pair of integers separated by -
.
No protocol name may be listed twice. No version may be listed twice for a single protocol. All versions must be in range 0 through 63 inclusive.
§impl FromStr for Signature
Decode a signature from hexadecimal.
impl FromStr for Signature
Decode a signature from hexadecimal.
Upper and lower case hexadecimal are both accepted, however mixed case is rejected.
§impl FromStr for u1
impl FromStr for u1
type Err = ParseIntError
§impl FromStr for u2
impl FromStr for u2
type Err = ParseIntError
§impl FromStr for u3
impl FromStr for u3
type Err = ParseIntError
§impl FromStr for u4
impl FromStr for u4
type Err = ParseIntError
§impl FromStr for u5
impl FromStr for u5
type Err = ParseIntError
§impl FromStr for u6
impl FromStr for u6
type Err = ParseIntError
§impl FromStr for u7
impl FromStr for u7
type Err = ParseIntError
§impl FromStr for u24
impl FromStr for u24
type Err = ParseIntError
§impl FromStr for u40
impl FromStr for u40
type Err = ParseIntError
§impl FromStr for u48
impl FromStr for u48
type Err = ParseIntError
§impl FromStr for u56
impl FromStr for u56
type Err = ParseIntError
§impl<Size> FromStr for EncodedPoint<Size>where
Size: ModulusSize,
Decode a SEC1-encoded point from hexadecimal.
impl<Size> FromStr for EncodedPoint<Size>where
Size: ModulusSize,
Decode a SEC1-encoded point from hexadecimal.
Upper and lower case hexadecimal are both accepted, however mixed case is rejected.