如何指定依赖项的确切版本?

How to specify the exact version of a dependency?

我正在使用

$ cargo --version
cargo 0.21.0-beta (7e00b82d9 2017-07-17)

我用cargo new --bin test1创建了一个简单的项目,然后我添加了一个依赖:

[dependencies]
lazy_static = "0.2.2"

到Cargo.toml(根据this这样的版本存在)和

#[macro_use]
extern crate lazy_static;

src/main.rs

当我运行cargo build:

$ cargo build
   Compiling lazy_static v0.2.8
   Compiling test1 v0.1.0 (file:///tmp/test1)
warning: unused `#[macro_use]` import
 --> src/main.rs:1:1
  |
1 | #[macro_use]
  | ^^^^^^^^^^^^
  |
  = note: #[warn(unused_imports)] on by default

    Finished dev [unoptimized + debuginfo] target(s) in 0.49 secs

为什么 cargo build 编译最后一个版本 0.2.8 而不是我指定的 0.2.2?我做错了什么?

长话短说:

my-crate = "=1.2.3"

阅读文档通常是个好主意。在这种情况下,Cargo documentation has an entire section on specifying dependencies:

Since this string does not have any operators in it, it is interpreted the same way as if we had specified "^0.1.12", which is called a caret requirement.

Caret requirements allow SemVer compatible updates to a specified version. An update is allowed if the new version number does not modify the left-most non-zero digit in the major, minor, patch grouping.

以及

Inequality requirements allow manually specifying a version range or an exact version to depend on.

Here are some examples of inequality requirements:

= 1.2.3

What am I doing wrong?

我要说的是,如果没有 情有可原 ,试图指定一个确切的版本是错误的。通常没有什么理由强迫您的代码用户使用旧版本的 crate 并阻止他们获得错误修复。

Cargo.lock 是避免使用不一致的依赖项集部署应用程序的正确工具。

精确版本前面有 =。它是 comparison requirements(以前称为不等式要求)之一。

lazy_static = "= 0.2.2"

默认是插入符要求(例如 0.2.2 等同于 ^0.2.2 ),它接受次要版本和补丁版本更新(如果主要是 0,则只接受补丁更新) .除非您有充分的理由不允许这样做,否则通常建议保留默认说明符。