Trait core::str::FromStr

1.0.0 · source ·
pub trait FromStr: Sized {
    type Err;

    // Required method
    fn from_str(s: &str) -> Result<Self, Self::Err>;
}
Expand description

解析字符串中的值

FromStr 的 from_str 方法通常通过 strparse 方法隐式使用。 有关示例,请参见 parse 的文档。

FromStr 没有生命周期参数,因此您只能解析本身不包含生命周期参数的类型。

换句话说,您可以使用 FromStr 解析 i32,但是不能解析 &i32。 您可以解析包含 i32 的结构体,但不能解析包含 &i32 的结构体。

Examples

FromStr 在示例 Point 类型上的基本实现:

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 });
// 显式调用
assert_eq!(Point::from_str("(1,2)"), expected);
// 隐式调用,通过解析
assert_eq!("(1,2)".parse(), expected);
assert_eq!("(1,2)".parse::<Point>(), expected);
// 输入字符串无效
assert!(Point::from_str("(1 2)").is_err());
Run

Required Associated Types§

source

type Err

可以从解析中返回的相关错误。

Required Methods§

source

fn from_str(s: &str) -> Result<Self, Self::Err>

解析字符串 s 以返回此类型的值。

如果解析成功,则返回 Ok 内部的值,否则,当字符串格式错误时,返回特定于 Err 内部的错误。 错误类型特定于 trait 的实现。

Examples

i32 的基本用法,一种实现 FromStr 的类型:

use std::str::FromStr;

let s = "5";
let x = i32::from_str(s).unwrap();

assert_eq!(5, x);
Run

Implementors§

1.7.0 · source§

impl FromStr for IpAddr

source§

impl FromStr for SocketAddr

source§

impl FromStr for bool

1.20.0 · source§

impl FromStr for char

source§

impl FromStr for f32

source§

impl FromStr for f64

source§

impl FromStr for i8

source§

impl FromStr for i16

source§

impl FromStr for i32

source§

impl FromStr for i64

source§

impl FromStr for i128

source§

impl FromStr for isize

source§

impl FromStr for u8

source§

impl FromStr for u16

source§

impl FromStr for u32

source§

impl FromStr for u64

source§

impl FromStr for u128

source§

impl FromStr for usize

source§

impl FromStr for Ipv4Addr

source§

impl FromStr for Ipv6Addr

1.5.0 · source§

impl FromStr for SocketAddrV4

1.5.0 · source§

impl FromStr for SocketAddrV6

1.35.0 · source§

impl FromStr for NonZeroI8

1.35.0 · source§

impl FromStr for NonZeroI16

1.35.0 · source§

impl FromStr for NonZeroI32

1.35.0 · source§

impl FromStr for NonZeroI64

1.35.0 · source§

impl FromStr for NonZeroI128

1.35.0 · source§

impl FromStr for NonZeroIsize

1.35.0 · source§

impl FromStr for NonZeroU8

1.35.0 · source§

impl FromStr for NonZeroU16

1.35.0 · source§

impl FromStr for NonZeroU32

1.35.0 · source§

impl FromStr for NonZeroU64

1.35.0 · source§

impl FromStr for NonZeroU128

1.35.0 · source§

impl FromStr for NonZeroUsize