开发依赖

有时,我们需要仅用于测试(或示例,或基准测试)的依赖项。这些依赖项在 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 关于指定依赖项的文档。