'_' で始まるフィーチャーは慣習的にそのクレート専用である。

よくある用法

結合テストやベンチマークでよく使われる。

サンプル

以下では、単体テストと結合テストの両方から呼べるメソッドを作成している。具体的には、非公開のフィーチャー _test を用意して、属性 #[cfg(feature = "_test")] をメソッド Item::margin に指定している (既存の #[cfg(test)] だと単体テスト専用になる)。

cargo.toml

[package]
name = "my_app"
version = "0.1.0"
edition = "2024"

[dev-dependencies]
my_app = { path = ".", features = ["_test"] }

[features]
_test = []
lib.rs

pub struct Item {
    pub price: u32,
    cost: u32,
}

impl Item {
    pub fn new(price: u32, cost: u32) -> Self {
        Self { price, cost }
    }

    #[cfg(feature = "_test")]
    pub fn margin(&self) -> u32 {
        self.price - self.cost
    }
}
tests/test.rs

use my_app::Item;

#[test]
fn test() {
    let item = Item::new(100, 10);
    assert_eq!(item.margin(), 90);
}