2.2.2.9 TryFrom 与 TryInto

09-TryFrom 与 TryInto — Comprehensive Rust

译文 · 基于 Comprehensive Rust

原文链接: https://google.github.io/comprehensive-rust/idiomatic/foundations-api-design/predictable-api/common-traits/try-from-into.html

2.2.2.9 TryFrom 与 TryInto

从一种类型到另一种类型的可失败转换。

可派生:❌

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Copyright 2025 Google LLC
// SPDX-License-Identifier: Apache-2.0
#[derive(Debug)]
pub struct InvalidNumber;

#[derive(Debug)]
pub struct DivisibleByTwo(usize);

impl TryFrom<usize> for DivisibleByTwo {
    type Error = InvalidNumber;

    fn try_from(value: usize) -> Result<Self, InvalidNumber> {
        if value.rem_euclid(2) == 0 {
            Ok(DivisibleByTwo(value))
        } else {
            Err(InvalidNumber)
        }
    }
}

fn main() {
    let success: Result<DivisibleByTwo, _> = 4.try_into();
    dbg!(success);
    let fail: Result<DivisibleByTwo, _> = 5.try_into();
    dbg!(fail);
}
  • 提供可能失败的转换,返回 result 类型。

  • 与 From/Into 一样,优先为类型实现 TryFrom 而非 TryInto。

  • 实现可指定 Result 的错误类型。

最后修改 August 11, 2026: 更新 (70a5af133)