@@ -51,7 +51,7 @@ versions before 1.0.0. While SemVer says there is no compatibility before
51
51
and ` x > 0 ` .
52
52
53
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 .
54
+ using special operators as described in the following section .
55
55
56
56
## Version requirement syntax
57
57
@@ -158,16 +158,17 @@ separated with a comma, e.g., `>= 1.2, < 1.5`.
158
158
159
159
## Specifying dependencies from other registries
160
160
161
- To specify a dependency from a registry other than [ crates.io] , first the
162
- registry must be configured in a ` .cargo/config.toml ` file. See the [ registries
163
- documentation] for more information. In the dependency, set the ` registry ` key
164
- to the name of the registry to use.
161
+ To specify a dependency from a registry other than [ crates.io] set the ` registry ` key
162
+ to the name of the registry to use:
165
163
166
164
``` toml
167
165
[dependencies ]
168
166
some-crate = { version = " 1.0" , registry = " my-registry" }
169
167
```
170
168
169
+ where ` my-registry ` is the registry name configured in ` .cargo/config.toml ` file.
170
+ See the [ registries documentation] for more information.
171
+
171
172
> ** Note** : [ crates.io] does not allow packages to be published with
172
173
> dependencies on code published outside of [ crates.io] .
173
174
@@ -183,69 +184,112 @@ you need to specify is the location of the repository with the `git` key:
183
184
regex = { git = " https://github.com/rust-lang/regex.git" }
184
185
```
185
186
186
- Cargo will fetch the ` git ` repository at this location then look for a
187
- ` Cargo.toml ` for the requested crate anywhere inside the ` git ` repository
188
- (not necessarily at the root --- for example, specifying a member crate name
189
- of a workspace and setting ` git ` to the repository containing the workspace).
187
+ Cargo fetches the ` git ` repository at that location, then looks for
188
+ ` Cargo.toml ` file for the requested crate anywhere inside the ` git ` repository.
189
+ For example, ` cpp ` and ` cpp_common ` are members of ` rust-cpp ` repo
190
+ and can be referred to by the repo's root URL (` https://github.com/mystor/rust-cpp ` ).
191
+
192
+ ``` toml
193
+ cpp = { git = " https://github.com/mystor/rust-cpp" }
194
+ cpp_common = { git = " https://github.com/mystor/rust-cpp" }
195
+ ```
196
+
197
+ The above rule does not apply to local paths specified via ` path ` attribute.
198
+
199
+ #### Choice of commit
190
200
191
- Since we haven’t specified any other information, Cargo assumes that
192
- we intend to use the latest commit on the default branch to build
193
- our package, which may not necessarily be the main branch.
194
- You can combine the ` git ` key with the ` rev ` , ` tag ` , or ` branch ` keys to
195
- specify something else. Here's an example of specifying that you want to use
196
- the latest commit on a branch named ` next ` :
201
+ Cargo assumes that we intend to use the latest commit on the default branch to build
202
+ our package if we only specify the repo URL, as in the examples above.
203
+
204
+ You can combine the ` git ` key with the ` rev ` , ` tag ` , or ` branch ` keys to be more specific about
205
+ which commit to use. Here's an example of using the latest commit on a branch named ` next ` :
197
206
198
207
``` toml
199
208
[dependencies ]
200
209
regex = { git = " https://github.com/rust-lang/regex.git" , branch = " next" }
201
210
```
202
211
203
- Anything that is not a branch or tag falls under ` rev ` . This can be a commit
212
+ Anything that is not a branch or a tag falls under ` rev ` key . This can be a commit
204
213
hash like ` rev = "4c59b707" ` , or a named reference exposed by the remote
205
- repository such as ` rev = "refs/pull/493/head" ` . What references are available
206
- varies by where the repo is hosted; GitHub in particular exposes a reference to
207
- the most recent commit of every pull request as shown, but other git hosts often
208
- provide something equivalent, possibly under a different naming scheme.
209
-
210
- Once a ` git ` dependency has been added, Cargo will lock that dependency to the
211
- latest commit at the time. New commits will not be pulled down automatically
212
- once the lock is in place. However, they can be pulled down manually with
213
- ` cargo update ` .
214
-
215
- See [ Git Authentication] for help with git authentication for private repos.
216
-
217
- > ** Note** : Neither the ` git ` key nor the ` path ` key changes the meaning of the
218
- > ` version ` key: the ` version ` key always implies that the package is available
219
- > in a registry. ` version ` , ` git ` , and ` path ` keys are considered [ separate
220
- > locations] ( #multiple-locations ) for resolving the dependency.
221
- >
222
- > When the dependency is retrieved from ` git ` , the ` version ` key will _ not_
223
- > affect which commit is used, but the version information in the dependency's
224
- > ` Cargo.toml ` file will still be validated against the ` version ` requirement.
214
+ repository such as ` rev = "refs/pull/493/head" ` .
215
+
216
+ What references are available for the ` rev ` key varies by where the repo is hosted.
217
+ GitHub exposes a reference to the most recent commit of every pull request as in the example above.
218
+ Other git hosts may provide something equivalent under a different naming scheme.
219
+
220
+ ** More ` git ` dependency examples:**
221
+
222
+ ``` toml
223
+ # .git suffix can be omitted if the host accepts such URLs - both examples work the same
224
+ regex = { git = " https://github.com/rust-lang/regex" }
225
+ regex = { git = " https://github.com/rust-lang/regex.git" }
226
+
227
+ # a commit with a particular tag
228
+ regex = { git = " https://github.com/rust-lang/regex" , tag = " 1.10.3" }
229
+
230
+ # a commit by its SHA1 hash
231
+ regex = { git = " https://github.com/rust-lang/regex" , rev = " 0c0990399270277832fbb5b91a1fa118e6f63dba" }
232
+
233
+ # HEAD commit of PR 493
234
+ regex = { git = " https://github.com/rust-lang/regex" , rev = " refs/pull/493/head" }
235
+
236
+ # INVALID EXAMPLES
237
+
238
+ # specifying the commit after # ignores the commit ID and generates a warning
239
+ regex = { git = " https://github.com/rust-lang/regex.git#4c59b70" }
240
+
241
+ # git and path cannot be used at the same time
242
+ regex = { git = " https://github.com/rust-lang/regex.git#4c59b70" , path = " ../regex" }
243
+ ```
244
+
245
+ Cargo locks the commits of ` git ` dependencies in ` Cargo.lock ` file at the time of their addition
246
+ and checks for updates only when you run ` cargo update ` command.
247
+
248
+ #### The role of _ version_ key
249
+
250
+ The ` version ` key always implies that the package is available in a registry,
251
+ regardless of the presence of ` git ` or ` path ` keys.
252
+
253
+ The ` version ` key does _ not_ affect which commit is used when Cargo retrieves the ` git ` dependency,
254
+ but Cargo checks the version information in the dependency's ` Cargo.toml ` file
255
+ against the ` version ` key and raises an error if the check fails.
256
+
257
+ In this example, Cargo retrieves the HEAD commit of the branch called ` next ` from Git and checks if the crate's version
258
+ is compatible with ` version = "1.10.3" ` :
259
+
260
+ ``` toml
261
+ [dependencies ]
262
+ regex = { version = " 1.10.3" , git = " https://github.com/rust-lang/regex" , branch = " next" }
263
+ ```
264
+
265
+ ` version ` , ` git ` , and ` path ` keys are considered separate locations for resolving the dependency.
266
+ See [ Multiple locations] ( #multiple-locations ) section below for detailed explanations.
225
267
226
268
> ** Note** : [ crates.io] does not allow packages to be published with
227
269
> dependencies on code published outside of [ crates.io] itself
228
270
> ([ dev-dependencies] are ignored). See the [ Multiple
229
271
> locations] ( #multiple-locations ) section for a fallback alternative for ` git `
230
272
> and ` path ` dependencies.
231
273
232
- [ Git Authentication ] : ../appendix/git-authentication.md
274
+ #### Accessing private Git repositories
275
+
276
+ See [ Git Authentication] ( ../appendix/git-authentication.md ) for help with Git authentication for private repos.
233
277
234
278
## Specifying path dependencies
235
279
236
280
Over time, our ` hello_world ` package from [ the guide] ( ../guide/index.md ) has
237
281
grown significantly in size! It’s gotten to the point that we probably want to
238
282
split out a separate crate for others to use. To do this Cargo supports ** path
239
283
dependencies** which are typically sub-crates that live within one repository.
240
- Let’s start off by making a new crate inside of our ` hello_world ` package:
284
+ Let’s start by making a new crate inside of our ` hello_world ` package:
241
285
242
286
``` console
243
287
# inside of hello_world/
244
288
$ cargo new hello_utils
245
289
```
246
290
247
291
This will create a new folder ` hello_utils ` inside of which a ` Cargo.toml ` and
248
- ` src ` folder are ready to be configured. In order to tell Cargo about this, open
292
+ ` src ` folder are ready to be configured. To tell Cargo about this, open
249
293
up ` hello_world/Cargo.toml ` and add ` hello_utils ` to your dependencies:
250
294
251
295
``` toml
@@ -254,30 +298,50 @@ hello_utils = { path = "hello_utils" }
254
298
```
255
299
256
300
This tells Cargo that we depend on a crate called ` hello_utils ` which is found
257
- in the ` hello_utils ` folder (relative to the ` Cargo.toml ` it’s written in).
301
+ in the ` hello_utils ` folder, relative to the ` Cargo.toml ` file it’s written in.
302
+
303
+ The next ` cargo build ` will automatically build ` hello_utils ` and
304
+ all of its dependencies.
305
+
306
+ #### No local path traversal
307
+
308
+ The local paths must point to the exact folder with the dependency's ` Cargo.toml ` .
309
+ Unlike with ` git ` dependencies, Cargo does not traverse local paths.
310
+ For example, if ` cpp ` and ` cpp_common ` are members of a locally cloned ` rust-cpp ` repo,
311
+ they have to be referred to by the full path:
312
+
313
+ ``` toml
314
+ # git key accepts the repo root URL and Cargo traverses the tree to find the crate
315
+ [dependencies ]
316
+ cpp = { git = " https://github.com/mystor/rust-cpp" }
317
+ cpp_common = { git = " https://github.com/mystor/rust-cpp" }
318
+
319
+ # path key requires the member name to be included in the local path
320
+ [dependencies ]
321
+ cpp = { path = " ../rust-cpp/cpp" }
322
+ cpp_common = { path = " ../rust-cpp/cpp_common" }
323
+ ```
324
+
325
+ #### Local paths in published crates
258
326
259
- And that’s it! The next ` cargo build ` will automatically build ` hello_utils ` and
260
- all of its own dependencies, and others can also start using the crate as well .
261
- However, crates that use dependencies specified with only a path are not
262
- permitted on [ crates.io ] . If we wanted to publish our ` hello_world ` crate, we
327
+ Crates that use dependencies specified with only a path are not
328
+ permitted on [ crates.io ] .
329
+
330
+ If we wanted to publish our ` hello_world ` crate, we
263
331
would need to publish a version of ` hello_utils ` to [ crates.io]
264
- and specify its version in the dependencies line as well :
332
+ as a separate crate and specify its version in the dependencies line of ` hello_world ` :
265
333
266
334
``` toml
267
335
[dependencies ]
268
336
hello_utils = { path = " hello_utils" , version = " 0.1.0" }
269
337
```
270
338
271
- > ** Note** : Neither the ` git ` key nor the ` path ` key changes the meaning of the
272
- > ` version ` key: the ` version ` key always implies that the package is available
273
- > in a registry. ` version ` , ` git ` , and ` path ` keys are considered [ separate
274
- > locations] ( #multiple-locations ) for resolving the dependency.
339
+ The use of ` path ` and ` version ` keys together is explained in the next section.
275
340
276
341
> ** Note** : [ crates.io] does not allow packages to be published with
277
- > dependencies on code published outside of [ crates.io] itself
278
- > ([ dev-dependencies] are ignored). See the [ Multiple
279
- > locations] ( #multiple-locations ) section for a fallback alternative for ` git `
280
- > and ` path ` dependencies.
342
+ > dependencies on code outside of [ crates.io] , except for [ dev-dependencies] .
343
+ > See the [ Multiple locations] ( #multiple-locations ) section
344
+ > for a fallback alternative for ` git ` and ` path ` dependencies.
281
345
282
346
## Multiple locations
283
347
0 commit comments