3 In Chapter 12, we built a package that included a binary crate and a library
4 crate. As your project develops, you might find that the library crate
5 continues to get bigger and you want to split your package further into
6 multiple library crates. Cargo offers a feature called *workspaces* that can
7 help manage multiple related packages that are developed in tandem.
9 ### Creating a Workspace
11 A *workspace* is a set of packages that share the same *Cargo.lock* and output
12 directory. Let’s make a project using a workspace—we’ll use trivial code so we
13 can concentrate on the structure of the workspace. There are multiple ways to
14 structure a workspace, so we'll just show one common way. We’ll have a
15 workspace containing a binary and two libraries. The binary, which will provide
16 the main functionality, will depend on the two libraries. One library will
17 provide an `add_one` function, and a second library an `add_two` function.
18 These three crates will be part of the same workspace. We’ll start by creating
19 a new directory for the workspace:
26 Next, in the *add* directory, we create the *Cargo.toml* file that will
27 configure the entire workspace. This file won’t have a `[package]` section.
28 Instead, it will start with a `[workspace]` section that will allow us to add
29 members to the workspace by specifying the path to the package with our binary
30 crate; in this case, that path is *adder*:
32 <span class="filename">Filename: Cargo.toml</span>
35 {{#include ../listings/ch14-more-about-cargo/no-listing-01-workspace-with-adder-crate/add/Cargo.toml}}
38 Next, we’ll create the `adder` binary crate by running `cargo new` within the
41 <!-- manual-regeneration
42 cd listings/ch14-more-about-cargo/output-only-01-adder-crate/add
50 Created binary (application) `adder` package
53 At this point, we can build the workspace by running `cargo build`. The files
54 in your *add* directory should look like this:
66 The workspace has one *target* directory at the top level that the compiled
67 artifacts will be placed into; the `adder` package doesn’t have its own
68 *target* directory. Even if we were to run `cargo build` from inside the
69 *adder* directory, the compiled artifacts would still end up in *add/target*
70 rather than *add/adder/target*. Cargo structures the *target* directory in a
71 workspace like this because the crates in a workspace are meant to depend on
72 each other. If each crate had its own *target* directory, each crate would have
73 to recompile each of the other crates in the workspace to place the artifacts
74 in its own *target* directory. By sharing one *target* directory, the crates
75 can avoid unnecessary rebuilding.
77 ### Creating the Second Package in the Workspace
79 Next, let’s create another member package in the workspace and call it
80 `add_one`. Change the top-level *Cargo.toml* to specify the *add_one* path in
83 <span class="filename">Filename: Cargo.toml</span>
86 {{#include ../listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/Cargo.toml}}
89 Then generate a new library crate named `add_one`:
91 <!-- manual-regeneration
92 cd listings/ch14-more-about-cargo/output-only-02-add-one/add
94 cargo new add_one --lib
99 $ cargo new add_one --lib
100 Created library `add_one` package
103 Your *add* directory should now have these directories and files:
119 In the *add_one/src/lib.rs* file, let’s add an `add_one` function:
121 <span class="filename">Filename: add_one/src/lib.rs</span>
124 {{#rustdoc_include ../listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/add_one/src/lib.rs}}
127 Now we can have the `adder` package with our binary depend on the `add_one`
128 package that has our library. First, we’ll need to add a path dependency on
129 `add_one` to *adder/Cargo.toml*.
131 <span class="filename">Filename: adder/Cargo.toml</span>
134 {{#include ../listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/adder/Cargo.toml:6:7}}
137 Cargo doesn’t assume that crates in a workspace will depend on each other, so
138 we need to be explicit about the dependency relationships.
140 Next, let’s use the `add_one` function (from the `add_one` crate) in the
141 `adder` crate. Open the *adder/src/main.rs* file and add a `use` line at the
142 top to bring the new `add_one` library crate into scope. Then change the `main`
143 function to call the `add_one` function, as in Listing 14-7.
145 <span class="filename">Filename: adder/src/main.rs</span>
148 {{#rustdoc_include ../listings/ch14-more-about-cargo/listing-14-07/add/adder/src/main.rs}}
151 <span class="caption">Listing 14-7: Using the `add_one` library crate from the
154 Let’s build the workspace by running `cargo build` in the top-level *add*
157 <!-- manual-regeneration
158 cd listings/ch14-more-about-cargo/listing-14-07/add
160 copy output below; the output updating script doesn't handle subdirectories in paths properly
165 Compiling add_one v0.1.0 (file:///projects/add/add_one)
166 Compiling adder v0.1.0 (file:///projects/add/adder)
167 Finished dev [unoptimized + debuginfo] target(s) in 0.68s
170 To run the binary crate from the *add* directory, we can specify which
171 package in the workspace we want to run by using the `-p` argument and the
172 package name with `cargo run`:
174 <!-- manual-regeneration
175 cd listings/ch14-more-about-cargo/listing-14-07/add
177 copy output below; the output updating script doesn't handle subdirectories in paths properly
182 Finished dev [unoptimized + debuginfo] target(s) in 0.0s
183 Running `target/debug/adder`
184 Hello, world! 10 plus one is 11!
187 This runs the code in *adder/src/main.rs*, which depends on the `add_one` crate.
189 #### Depending on an External Package in a Workspace
191 Notice that the workspace has only one *Cargo.lock* file at the top level,
192 rather than having a *Cargo.lock* in each crate’s directory. This ensures that
193 all crates are using the same version of all dependencies. If we add the `rand`
194 package to the *adder/Cargo.toml* and *add_one/Cargo.toml* files, Cargo will
195 resolve both of those to one version of `rand` and record that in the one
196 *Cargo.lock*. Making all crates in the workspace use the same dependencies
197 means the crates will always be compatible with each other. Let’s add the
198 `rand` crate to the `[dependencies]` section in the *add_one/Cargo.toml* file
199 so we can use the `rand` crate in the `add_one` crate:
201 <!-- When updating the version of `rand` used, also update the version of
202 `rand` used in these files so they all match:
203 * ch02-00-guessing-game-tutorial.md
204 * ch07-04-bringing-paths-into-scope-with-the-use-keyword.md
207 <span class="filename">Filename: add_one/Cargo.toml</span>
210 {{#include ../listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/add_one/Cargo.toml:6:7}}
213 We can now add `use rand;` to the *add_one/src/lib.rs* file, and building the
214 whole workspace by running `cargo build` in the *add* directory will bring in
215 and compile the `rand` crate. We will get one warning because we aren’t
216 referring to the `rand` we brought into scope:
218 <!-- manual-regeneration
219 cd listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add
221 copy output below; the output updating script doesn't handle subdirectories in paths properly
226 Updating crates.io index
227 Downloaded rand v0.8.3
229 Compiling rand v0.8.3
230 Compiling add_one v0.1.0 (file:///projects/add/add_one)
231 warning: unused import: `rand`
232 --> add_one/src/lib.rs:1:5
237 = note: `#[warn(unused_imports)]` on by default
239 warning: 1 warning emitted
241 Compiling adder v0.1.0 (file:///projects/add/adder)
242 Finished dev [unoptimized + debuginfo] target(s) in 10.18s
245 The top-level *Cargo.lock* now contains information about the dependency of
246 `add_one` on `rand`. However, even though `rand` is used somewhere in the
247 workspace, we can’t use it in other crates in the workspace unless we add
248 `rand` to their *Cargo.toml* files as well. For example, if we add `use rand;`
249 to the *adder/src/main.rs* file for the `adder` package, we’ll get an error:
251 <!-- manual-regeneration
252 cd listings/ch14-more-about-cargo/output-only-03-use-rand/add
254 copy output below; the output updating script doesn't handle subdirectories in paths properly
260 Compiling adder v0.1.0 (file:///projects/add/adder)
261 error[E0432]: unresolved import `rand`
262 --> adder/src/main.rs:2:5
265 | ^^^^ no external crate `rand`
268 To fix this, edit the *Cargo.toml* file for the `adder` package and indicate
269 that `rand` is a dependency for it as well. Building the `adder` package will
270 add `rand` to the list of dependencies for `adder` in *Cargo.lock*, but no
271 additional copies of `rand` will be downloaded. Cargo has ensured that every
272 crate in every package in the workspace using the `rand` package will be using
273 the same version, saving us space and ensuring that the crates in the workspace
274 will be compatible with each other.
276 #### Adding a Test to a Workspace
278 For another enhancement, let’s add a test of the `add_one::add_one` function
279 within the `add_one` crate:
281 <span class="filename">Filename: add_one/src/lib.rs</span>
284 {{#rustdoc_include ../listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/add_one/src/lib.rs}}
287 Now run `cargo test` in the top-level *add* directory. Running `cargo test` in
288 a workspace structured like this one will run the tests for all the crates in
291 <!-- manual-regeneration
292 cd listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add
294 copy output below; the output updating script doesn't handle subdirectories in
300 Compiling add_one v0.1.0 (file:///projects/add/add_one)
301 Compiling adder v0.1.0 (file:///projects/add/adder)
302 Finished test [unoptimized + debuginfo] target(s) in 0.27s
303 Running target/debug/deps/add_one-f0253159197f7841
306 test tests::it_works ... ok
308 test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
310 Running target/debug/deps/adder-49979ff40686fa8e
314 test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
320 test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
323 The first section of the output shows that the `it_works` test in the `add_one`
324 crate passed. The next section shows that zero tests were found in the `adder`
325 crate, and then the last section shows zero documentation tests were found in
328 We can also run tests for one particular crate in a workspace from the
329 top-level directory by using the `-p` flag and specifying the name of the crate
332 <!-- manual-regeneration
333 cd listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add
334 cargo test -p add_one
335 copy output below; the output updating script doesn't handle subdirectories in paths properly
339 $ cargo test -p add_one
340 Finished test [unoptimized + debuginfo] target(s) in 0.00s
341 Running target/debug/deps/add_one-b3235fea9a156f74
344 test tests::it_works ... ok
346 test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
352 test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
355 This output shows `cargo test` only ran the tests for the `add_one` crate and
356 didn’t run the `adder` crate tests.
358 If you publish the crates in the workspace to [crates.io](https://crates.io/),
359 each crate in the workspace will need to be published separately. Like `cargo
360 test`, we can publish a particular crate in our workspace by using the `-p`
361 flag and specifying the name of the crate we want to publish.
363 For additional practice, add an `add_two` crate to this workspace in a similar
364 way as the `add_one` crate!
366 As your project grows, consider using a workspace: it’s easier to understand
367 smaller, individual components than one big blob of code. Furthermore, keeping
368 the crates in a workspace can make coordination between crates easier if they
369 are often changed at the same time.