03-`ToString` 和 `FromStr`

ToString 和 FromStr — Rust By Example

译文 · 基于 Rust By Example

原文链接: https://doc.rust-lang.org/stable/rust-by-example/conversion/string.html

ToString 和 FromStr

ToString

​ 要把任何类型转换成 String,只需要实现那个类型的 ToString trait。然而不要直接这么做,您应该实现fmt::Display trait,它会自动提供 ToString,并且还可以用来打印类型,就像 print! 一节中讨论的那样。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
use std::fmt;

struct Circle {
    radius: i32
}

impl fmt::Display for Circle {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Circle of radius {}", self.radius)
    }
}

fn main() {
    let circle = Circle { radius: 6 };
    println!("{}", circle.to_string());//Circle of radius 6
}

译注:一个实现 ToString 的例子

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
use std::string::ToString;

struct Circle {
    radius: i32
}

impl ToString for Circle {
    fn to_string(&self) -> String {
        format!("Circle of radius {:?}", self.radius)
    }
}

fn main() {
    let circle = Circle { radius: 6 };
    println!("{}", circle.to_string());//Circle of radius 6
}

解析字符串

​ 将字符串转换为多种类型是很有用的,但字符串操作中比较常见的一种是将字符串转换为数字。实现这一操作的惯用方法是使用parse函数,要么通过类型推断来实现,要么使用“涡轮鱼”语法指定要解析的类型。以下示例展示了这两种方法。

​ 只要为目标类型实现了 FromStr trait,该方法就会将字符串转换为指定类型。标准库中已为众多类型实现了这一trait。

1
2
3
4
5
6
7
fn main() {
    let parsed: i32 = "5".parse().unwrap();
    let turbo_parsed = "10".parse::<i32>().unwrap();

    let sum = parsed + turbo_parsed;
    println!{"Sum: {:?}", sum};//Sum: 15
}

​ 要在用户定义的类型上实现此功能,只需为该类型实现FromStr trait 即可。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use std::num::ParseIntError;
use std::str::FromStr;

#[derive(Debug)]
struct Circle {
    radius: i32,
}

impl FromStr for Circle {
    type Err = ParseIntError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.trim().parse() {
            Ok(num) => Ok(Circle{ radius: num }),
            Err(e) => Err(e),
        }
    }
}

fn main() {
    let radius = "    3 ";
    let circle: Circle = radius.parse().unwrap();
    println!("{:?}", circle);//Circle { radius: 3 }
}
最后修改 October 2, 2026: 更新 (f3447ada6)