简体   繁体   English

Rust Cargo 中的插入符号版本约束是什么意思?

[英]What does a caret version constraint mean in Rust Cargo?

I'm implementing Rust Cargo version requirements.我正在实施 Rust Cargo 版本要求。 In general, I have trouble understanding caret requirements as specified.一般来说,我很难理解指定的插入符号要求 I found What's the difference between tilde(~) and caret(^) in package.json?我发现package.json 中的波浪号(~)和插入符号(^)有什么区别? , but this question is about npm version requirements of which I'm not sure if it is the same as Rust Cargo version requirements. ,但是这个问题是关于 npm 版本要求的,我不确定它是否与 Rust Cargo 版本要求相同。

At https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#caret-requirements , I read:https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#caret-requirements ,我读到:

Caret requirements allow SemVer compatible updates to a specified version. Caret 要求允许 SemVer 兼容更新到指定版本。 An update is allowed if the new version number does not modify the left-most non-zero digit in the major, minor, patch grouping.如果新版本号没有修改主要、次要、补丁分组中最左边的非零数字,则允许更新。 In this case, if we ran cargo update -p time , cargo should update us to version 0.1.13 if it is the latest 0.1.z release, but would not update us to 0.2.0 .在这种情况下,如果我们运行cargo update -p time ,如果它是最新的0.1.z版本,cargo 应该将我们更新到版本0.1.13 ,但不会将我们更新到0.2.0 If instead we had specified the version string as ^1.0 , cargo should update to 1.1 if it is the latest 1.y release, but not 2.0 .相反,如果我们将版本字符串指定为^1.0 ,那么如果它是最新的1.y版本,cargo 应该更新为1.1 ,而不是2.0 The version 0.0.x is not considered compatible with any other version. 0.0.x版本不被视为与任何其他版本兼容。

Here are some more examples of caret requirements and the versions that would be allowed with them:以下是插入符号要求的更多示例以及它们允许的版本:

 ^1.2.3:= >=1.2.3, <2.0.0 ^1.2:= >=1.2.0, <2.0.0 ^1:= >=1.0.0, <2.0.0 ^0.2.3:= >=0.2.3, <0.3.0 ^0.2:= >=0.2.0, <0.3.0 ^0.0.3:= >=0.0.3, <0.0.4 ^0.0:= >=0.0.0, <0.1.0 ^0:= >=0.0.0, <1.0.0

This compatibility convention is different from SemVer in the way it treats versions before 1.0.0 .此兼容性约定与 SemVer 处理1.0.0之前版本的方式不同。 While SemVer says there is no compatibility before 1.0.0 , Cargo considers 0.xy to be compatible with 0.xz , where y ≥ z and x > 0 .虽然 SemVer 表示1.0.0之前没有兼容性,但 Cargo 认为0.xy0.xz兼容,其中y ≥ zx > 0

I'm confused about我很困惑

In this case, if we ran cargo update -p time , cargo should update us to version 0.1.13 if it is the latest 0.1.z release, but would not update us to 0.2.0 .在这种情况下,如果我们运行cargo update -p time ,如果它是最新的0.1.z版本,cargo 应该将我们更新到版本0.1.13 ,但不会将我们更新到0.2.0

With what version requirement is this the case?这种情况是什么版本要求? It seems left out of the sentence.它似乎被排除在句子之外。 It continues to refer to a seemingly missing caret version requirement:它继续引用看似缺失的插入符号版本要求:

If instead we had specified the version string as ^1.0 , cargo should update to 1.1 if it is the latest 1.y release, but not 2.0 .相反,如果我们将版本字符串指定为^1.0 ,那么如果它是最新的1.y版本,cargo 应该更新为1.1 ,而不是2.0

In which it refers to >1.0 as the version string (which I miss in the sentence before).其中将>1.0称为版本字符串(我在之前的句子中错过了它)。

If I dissect the examples, my reasoning is as follows:如果我剖析这些例子,我的推理如下:

^1.2.3  :=  >=1.2.3, <2.0.0 // same as >=1.2.3 AND 1.*
^1.2    :=  >=1.2.0, <2.0.0 // same as >=1.2 AND 1.*, which condenses into 1.2.*
^1      :=  >=1.0.0, <2.0.0 // same as 1.*
^0.2.3  :=  >=0.2.3, <0.3.0 // same as >=0.2.3 AND 0.2.*
^0.2    :=  >=0.2.0, <0.3.0 // same as 0.2.*, which condenses into 0.2.*
^0.0.3  :=  >=0.0.3, <0.0.4 // huh
^0.0    :=  >=0.0.0, <0.1.0 // same as >=0.0.0 AND 0.0.*
^0      :=  >=0.0.0, <1.0.0 // same as >=0.0.0 AND 0.*, which condenses into 0.*

So except for my understanding of the 6th example ( ^0.0.3 ), my conclusion is that caret version requirements are exactly as wildcard version requirements, except when PATCH is specified in which the wildcard version requirement ANDs with the >= {version} (equal or later then) comparison version requirement.因此,除了我对第 6 个示例 ( ^0.0.3 ) 的理解之外,我的结论是插入符号版本要求与通配符版本要求完全相同,除非指定 PATCH 时通配符版本要求与>= {version} (相等或之后)比较版本要求。

Is this understanding correct and why is example 6 as it is?这种理解是否正确,为什么示例 6 是这样的?

Unlike in npm, the default version requirement range is indeed the caret requirement!与 npm 不同,默认的版本要求范围确实是插入符要求! This is stated in the Cargo reference on "specifying dependencies" , just before the section linked in the question.在“指定依赖项”的货物参考中有所说明,就在问题中链接的部分之前。

The string "0.1.12" is a semver version requirement.字符串"0.1.12"是 semver 版本要求。 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.由于该字符串中没有任何运算符,因此它的解释方式与我们指定"^0.1.12"的方式相同,这称为插入符要求。

As such, the following two dependency specifications are equivalent.因此,以下两个依赖规范是等价的。

time = "0.1.12"
time = "^0.1.12"

This, by the way, is the requirement referred to in the rest of the document.顺便说一句,这是文档的 rest 中提到的要求。 An update of time could bring in a version higher than 0.1.12, but never 0.2.0 or higher. time更新可能会带来高于 0.1.12 的版本,但绝不会带来 0.2.0 或更高的版本。

See also:也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM