Skip to content

Commit f9089fc

Browse files
committed
Auto merge of #10158 - matklad:depodoc, r=ehuss
doc: nudge towards simple version requirements Cargo is very ingeniously designed such that the simplest thing you can do when sepcifying a dependency, `time = "1.2.3"`, is also the best thing you can do. However, new Rust folks often overthink this, and use more verbose syntaxes, which at best is unnecessary, and and at worst creates pains downstream. For example, https://github.com/elastic/elasticsearch-rs/blob/master/elasticsearch/Cargo.toml contains a mistrue of default, `~` and `^` requirements, which lets the reader wondering what's going on. Let's try to help the situation by focusing on default requirements in the docs
2 parents 0507d9f + ec3909e commit f9089fc

File tree

3 files changed

+33
-30
lines changed

3 files changed

+33
-30
lines changed

src/doc/src/guide/dependencies.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ use. This example adds a dependency of the `time` crate:
2020
time = "0.1.12"
2121
```
2222

23-
The version string is a [semver] version requirement. The [specifying
23+
The version string is a [SemVer] version requirement. The [specifying
2424
dependencies](../reference/specifying-dependencies.md) docs have more information about
2525
the options you have here.
2626

27-
[semver]: https://github.com/steveklabnik/semver#requirements
27+
[SemVer]: https://semver.org
2828

2929
If we also wanted to add a dependency on the `regex` crate, we would not need
3030
to add `[dependencies]` for each crate listed. Here's what your whole

src/doc/src/reference/registries.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,9 @@ explaining the format of the entry.
222222
// this is the new name. The original package name is stored in
223223
// the `package` field.
224224
"name": "rand",
225-
// The semver requirement for this dependency.
225+
// The SemVer requirement for this dependency.
226226
// This must be a valid version requirement defined at
227-
// https://github.com/steveklabnik/semver#requirements.
227+
// https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html.
228228
"req": "^0.6",
229229
// Array of features (as strings) enabled for this dependency.
230230
"features": ["i128_support"],

src/doc/src/reference/specifying-dependencies.md

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,45 @@ guide](../guide/index.md), we specified a dependency on the `time` crate:
1919
time = "0.1.12"
2020
```
2121

22-
The string `"0.1.12"` is a [semver] version requirement. Since this
23-
string does not have any operators in it, it is interpreted the same way as
24-
if we had specified `"^0.1.12"`, which is called a caret requirement.
25-
26-
[semver]: https://github.com/steveklabnik/semver#requirements
27-
28-
### Caret requirements
29-
30-
**Caret requirements** allow SemVer compatible updates to a specified version.
31-
An update is allowed if the new version number does not modify the left-most
32-
non-zero digit in the major, minor, patch grouping. In this case, if we ran
33-
`cargo update -p time`, cargo should update us to version `0.1.13` if it is the
34-
latest `0.1.z` release, but would not update us to `0.2.0`. If instead we had
35-
specified the version string as `^1.0`, cargo should update to `1.1` if it is
36-
the latest `1.y` release, but not `2.0`. The version `0.0.x` is not considered
37-
compatible with any other version.
38-
39-
Here are some more examples of caret requirements and the versions that would
22+
The string `"0.1.12"` is a version requirement. Although it looks like a
23+
specific *version* of the `time` crate, it actually specifies a *range* of
24+
versions and allows [SemVer] compatible updates. An update is allowed if the new
25+
version number does not modify the left-most non-zero digit in the major, minor,
26+
patch grouping. In this case, if we ran `cargo update -p time`, cargo should
27+
update us to version `0.1.13` if it is the latest `0.1.z` release, but would not
28+
update us to `0.2.0`. If instead we had specified the version string as `1.0`,
29+
cargo should update to `1.1` if it is the latest `1.y` release, but not `2.0`.
30+
The version `0.0.x` is not considered compatible with any other version.
31+
32+
[SemVer]: https://semver.org
33+
34+
Here are some more examples of version requirements and the versions that would
4035
be allowed with them:
4136

4237
```notrust
43-
^1.2.3 := >=1.2.3, <2.0.0
44-
^1.2 := >=1.2.0, <2.0.0
45-
^1 := >=1.0.0, <2.0.0
46-
^0.2.3 := >=0.2.3, <0.3.0
47-
^0.2 := >=0.2.0, <0.3.0
48-
^0.0.3 := >=0.0.3, <0.0.4
49-
^0.0 := >=0.0.0, <0.1.0
50-
^0 := >=0.0.0, <1.0.0
38+
1.2.3 := >=1.2.3, <2.0.0
39+
1.2 := >=1.2.0, <2.0.0
40+
1 := >=1.0.0, <2.0.0
41+
0.2.3 := >=0.2.3, <0.3.0
42+
0.2 := >=0.2.0, <0.3.0
43+
0.0.3 := >=0.0.3, <0.0.4
44+
0.0 := >=0.0.0, <0.1.0
45+
0 := >=0.0.0, <1.0.0
5146
```
5247

5348
This compatibility convention is different from SemVer in the way it treats
5449
versions before 1.0.0. While SemVer says there is no compatibility before
5550
1.0.0, Cargo considers `0.x.y` to be compatible with `0.x.z`, where `y ≥ z`
5651
and `x > 0`.
5752

53+
It is possible to further tweak the logic for selecting compatible versions
54+
using special operators, though it shouldn't be necessary most of the time.
55+
56+
### Caret requirements
57+
58+
**Caret requirements** are an alternative syntax for the default strategy,
59+
`^1.2.3` is exactly equivalent to `1.2.3`.
60+
5861
### Tilde requirements
5962

6063
**Tilde requirements** specify a minimal version with some ability to update.

0 commit comments

Comments
 (0)