第6章 类型转换

类型转换 — Rust By Example

译文 · 基于 Rust By Example

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

类型转换

​ Rust 使用 trait 解决类型之间的转换问题。最一般的转换会用到 From 和 Into 两个 trait。不过,即便常见的情况也可能会用到特别的 trait,尤其是从 String 转换到别的类型,以及把别的类型转换到 String 时。


01-`From` 和 `Into`

From 和 Into — Rust By Example

02-`TryFrom` 和 `TryInto`

TryFrom 和 TryInto — Rust By Example

03-`ToString` 和 `FromStr`

ToString 和 FromStr — Rust By Example

最后修改 October 2, 2026: 更新 (f3447ada6)