开发依赖 (Development dependencies)

有时仅需为测试(或示例、基准测试)添加依赖项。这类依赖项应添加到 Cargo.toml 文件中的 [dev-dependencies] 部分。这些依赖项不会传递给依赖此包的其他包。

一个这样的例子是 pretty_assertions,它扩展了标准的 assert_eq!assert_ne! 宏,以提供彩色的差异比较。
文件 Cargo.toml

# standard crate data is left out
[dev-dependencies]
pretty_assertions = "1"

文件 src/lib.rs

pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

#[cfg(test)]
mod tests {
    use super::*;
    use pretty_assertions::assert_eq; // crate for test-only use. Cannot be used in non-test code.

    #[test]
    fn test_add() {
        assert_eq!(add(2, 3), 5);
    }
}

另见

关于指定依赖项的 Cargo 文档