Skip to content

Commit 48aa53f

Browse files
committed
fix: Improve guide's dependency example
1 parent 14009d1 commit 48aa53f

File tree

1 file changed

+11
-24
lines changed

1 file changed

+11
-24
lines changed

text/3424-cargo-script.md

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -137,44 +137,26 @@ To depend on a library hosted on [crates.io], you modify `hello_world.rs`:
137137

138138
//! ```cargo
139139
//! [dependencies]
140-
//! time = "0.1.12"
140+
//! regex = "1.8.0"
141141
//! ```
142142

143143
fn main() {
144-
println!("Hello, world!");
144+
let re = Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
145+
println!("Did our date match? {}", re.is_match("2014-01-01"));
145146
}
146147
```
147148

148149
The `cargo` section is called a [***manifest***][def-manifest], and it contains all of the
149150
metadata that Cargo needs to compile your package. This is written in the
150151
[TOML] format (pronounced /tɑməl/).
151152

152-
`time = "0.1.12"` is the name of the [crate][def-crate] and a [SemVer] version
153+
`regex = "1.8.0"` is the name of the [crate][def-crate] and a [SemVer] version
153154
requirement. The [specifying
154155
dependencies](https://doc.rust-lang.org/cargo/guide/../reference/specifying-dependencies.html) docs have more
155156
information about the options you have here.
156157

157-
If we also wanted to add a dependency on the `regex` crate, we would not need
158-
to add `[dependencies]` for each crate listed. Here's what your whole
159-
`hello_world.rs` file would look like with dependencies on the `time` and `regex`
160-
crates:
161-
162-
```rust
163-
#!/usr/bin/env cargo
164-
165-
//! ```cargo
166-
//! [dependencies]
167-
//! time = "0.1.12"
168-
//! regex = "0.1.41"
169-
//! ```
170-
171-
fn main() {
172-
let re = Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
173-
println!("Did our date match? {}", re.is_match("2014-01-01"));
174-
}
175-
```
176-
177-
You can then re-run this and Cargo will fetch the new dependencies and all of their dependencies. You can see this by passing in `--verbose`:
158+
You can then re-run this and Cargo will fetch the new dependencies and all of
159+
their dependencies. You can see this by passing in `--verbose`:
178160
```console
179161
$ cargo --verbose ./hello_world.rs
180162
Updating crates.io index
@@ -194,6 +176,11 @@ $ cargo --verbose ./hello_world.rs
194176
Did our date match? true
195177
```
196178

179+
Cargo will cache the exact information about which revision of all of these dependencies we used.
180+
181+
Now, if `regex` gets updated, we will still build with the same revision until
182+
we choose to `cargo update --manifest-path hello_world.rs`.
183+
197184
## Package Layout
198185

199186
*(Adapted from [the cargo book](https://doc.rust-lang.org/cargo/guide/project-layout.html))*

0 commit comments

Comments
 (0)