07-`newtype` 惯用法

newtype 惯用法 — Rust By Example

译文 · 基于 Rust By Example

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

newtype 惯用法

newtype 惯用法(译注:即为不同种类的数据分别定义新的类型)能保证在编译时,提供给程序的都是正确的类型。

比如说,实现一个 “年龄认证” 函数,它要求输入必须是 Years 类型。

 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
27
28
struct Years(i64);

struct Days(i64);

impl Years {
    pub fn to_days(&self) -> Days {
        Days(self.0 * 365)
    }
}

impl Days {
    /// 舍去不满一年的部分
    pub fn to_years(&self) -> Years {
        Years(self.0 / 365)
    }
}

fn old_enough(age: &Years) -> bool {
    age.0 >= 18
}

fn main() {
    let age = Years(5);
    let age_days = age.to_days();
    println!("Old enough {}", old_enough(&age));
    println!("Old enough {}", old_enough(&age_days.to_years()));
    // println!("Old enough {}", old_enough(&age_days));
}

取消最后一行的注释,就可以发现提供给 old_enough 的必须是 Years 类型。

See also:

structs

最后修改 August 21, 2026: 更新 (76fc81a2e)