如何指定依赖项的确切版本? [英] How to specify the exact version of a dependency?

查看:62
本文介绍了如何指定依赖项的确切版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

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

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

[依赖项]lazy_static = "0.2.2"

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

#[macro_use]extern crate lazy_static;

src/main.rs

当我运行 cargo build 时:

$ cargo build编译lazy_static v0.2.8编译 test1 v0.1.0 (file:///tmp/test1)警告:未使用的`#[macro_use]` 导入-->src/main.rs:1:1|1 |#[宏使用]|^^^^^^^^^^^^^|= 注意:#[warn(unused_imports)] 默认开启在 0.49 秒内完成开发 [未优化 + 调试信息] 目标

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

解决方案

TL;DR:

my-crate = "=1.2.3";


阅读文档通常是个好主意.在这种情况下,货物文档有一整节关于指定依赖项:

<块引用>

由于此字符串中没有任何运算符,因此它的解释方式与我们指定 "^0.1.12" 的方式相同,这称为脱字符要求.

Caret 要求允许将 SemVer 兼容更新到指定版本.如果新版本号没有修改主要、次要、补丁分组中最左边的非零数字,则允许更新.

以及

<块引用>

不等式要求允许手动指定要依赖的版本范围或确切版本.

以下是不等式要求的一些示例:

= 1.2.3


<块引用>

我做错了什么?

我想说,在没有情有可原的情况的情况下,试图指定一个确切的版本是错误的.通常几乎没有理由强迫您的代码用户使用旧版本的 crate 并阻止他们获得错误修复.

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

I'm using

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

I created a simple project with cargo new --bin test1, and then I added a dependency:

[dependencies]
lazy_static = "0.2.2"

to Cargo.toml (according to this such version exists) and

#[macro_use]
extern crate lazy_static;

to src/main.rs

When I run 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

Why does cargo build compile last version 0.2.8 instead of 0.2.2 that I specified? What am I doing wrong?

解决方案

TL;DR:

my-crate = "=1.2.3"


Reading the documentation is generally a great idea. In this case, the 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.

As well as

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?

I'd say that, without extenuating circumstances, trying to specify an exact version is wrong. There's generally very little reason to force the users of your code to be stuck to an older version of a crate and prevent them from getting bug fixes.

Cargo.lock is the correct tool to avoid deploying your application with an inconsistent set of dependencies.

这篇关于如何指定依赖项的确切版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆