2.2.1.2 Is

02-Is — Comprehensive Rust

译文 · 基于 Comprehensive Rust

原文链接: https://google.github.io/comprehensive-rust/idiomatic/foundations-api-design/predictable-api/naming-conventions/is.html

2.2.1.2 Is

检查数据类型上的某个条件。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// Copyright 2025 Google LLC
// SPDX-License-Identifier: Apache-2.0
impl<T> Vec<T> {
    fn is_empty(&self) -> bool;
}

impl f32 {
    fn is_nan(self) -> bool;
}

impl u32 {
    fn is_power_of_two(self) -> bool;
}
  • 对某个值的布尔条件。

  • 比起名称中带 not 的方法,更偏好 is 前缀。标准库方法中没有 is_not_ 的实例,直接用 !value.is_[condition]。

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