|
| 1 | +# Creating a New Workspace |
| 2 | + |
| 3 | +A [workspace][def-workspace] is a collection of one or more packages, |
| 4 | +called workspace members, that are managed together. |
| 5 | + |
| 6 | +In this chapter, we will create a workspace `new_workspace` containing |
| 7 | +binary member `foo` and library member `bar`. |
| 8 | + |
| 9 | +As mentioned in [`[workspace]` section][workspace-section], the workspace must |
| 10 | +have at least one member, either the [root package] or a [virtual manifest]. |
| 11 | + |
| 12 | +Next we create a workspace containing [root package]. |
| 13 | +For convenience, you can first create a package using the command `cargo new new_workspace`. |
| 14 | +Then add the `[workspace]` section to the `Cargo.toml` file in the root directory |
| 15 | +to make it a manifest of the workspace: |
| 16 | + |
| 17 | +```toml |
| 18 | +# [new_workspace]/Cargo.toml |
| 19 | +[workspace] |
| 20 | + |
| 21 | +[package] |
| 22 | +name = "new_workspace" |
| 23 | +version = "0.1.0" |
| 24 | +edition = "2021" |
| 25 | + |
| 26 | +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
| 27 | + |
| 28 | +[dependencies] |
| 29 | +``` |
| 30 | + |
| 31 | +Then, continue adding members `foo` and `bar` to the workspace: |
| 32 | + |
| 33 | +```console |
| 34 | +$ cd new_workspace |
| 35 | +$ cargo new foo |
| 36 | +$ cargo new bar --lib |
| 37 | +``` |
| 38 | + |
| 39 | +Cargo will automatically add members to `Cargo.toml`。 |
| 40 | +At this point, the workspace will contain three members: `foo` and `bar` and |
| 41 | +the default member `new_workspace`. |
| 42 | + |
| 43 | +```toml |
| 44 | +# [new_workspace]/Cargo.toml |
| 45 | +[workspace] |
| 46 | +members = [ "bar", "foo" ] |
| 47 | + |
| 48 | +[package] |
| 49 | +name = "new_workspace" |
| 50 | +version = "0.1.0" |
| 51 | +edition = "2021" |
| 52 | + |
| 53 | +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
| 54 | + |
| 55 | +[dependencies] |
| 56 | +``` |
| 57 | + |
| 58 | +The package at this point contains the following files: |
| 59 | + |
| 60 | +```console |
| 61 | +$ cd new_workspace |
| 62 | +$ tree . |
| 63 | +. |
| 64 | +├── bar |
| 65 | +│ ├── Cargo.toml |
| 66 | +│ └── src |
| 67 | +│ └── lib.rs |
| 68 | +├── Cargo.toml |
| 69 | +├── foo |
| 70 | +│ ├── Cargo.toml |
| 71 | +│ └── src |
| 72 | +│ └── main.rs |
| 73 | +└── src |
| 74 | + └── main.rs |
| 75 | + |
| 76 | +5 directories, 6 files |
| 77 | +``` |
| 78 | + |
| 79 | +Let's move on and create a virtual workspace. |
| 80 | + |
| 81 | +In the another `new_workspace` empty directory, create a new `Cargo.toml` file and |
| 82 | +add the `[workspace]` section: |
| 83 | + |
| 84 | +```toml |
| 85 | +# [new_workspace]/Cargo.toml |
| 86 | +[workspace] |
| 87 | +``` |
| 88 | + |
| 89 | +If using a virtual workspace, then the version of [resolver] needs to be specified |
| 90 | +in the table (if not, the default version of resolver for a workspace is `1`, |
| 91 | +even if the default resolver version for workspace members is `2`), for example: |
| 92 | + |
| 93 | +```toml |
| 94 | +# [new_workspace]/Cargo.toml |
| 95 | +[workspace] |
| 96 | +resolver = "2" |
| 97 | +``` |
| 98 | + |
| 99 | +Likewise, you can then use the `cargo new <package>` command to create |
| 100 | +binary member `foo` and library member `bar`. |
| 101 | + |
| 102 | + |
| 103 | +```toml |
| 104 | +# [new_workspace]/Cargo.toml |
| 105 | +[workspace] |
| 106 | +resolver = "2" |
| 107 | +members = [ "bar","foo"] |
| 108 | + |
| 109 | +``` |
| 110 | + |
| 111 | +The package at this point contains the following files: |
| 112 | + |
| 113 | +```console |
| 114 | +$ cd new_workspace |
| 115 | +$ tree . |
| 116 | +. |
| 117 | +├── bar |
| 118 | +│ ├── Cargo.toml |
| 119 | +│ └── src |
| 120 | +│ └── lib.rs |
| 121 | +├── Cargo.toml |
| 122 | +└── foo |
| 123 | + ├── Cargo.toml |
| 124 | + └── src |
| 125 | + └── main.rs |
| 126 | + |
| 127 | +4 directories, 5 files |
| 128 | +``` |
| 129 | + |
| 130 | +Up to this point, we have a workspace with two members. |
| 131 | +Whenever you run `cargo build` under the workspace root directory, Cargo builds |
| 132 | +all member at once. |
| 133 | +Instead of building the entire workspace, you could use the `--package`/`-p` flag |
| 134 | +to select certain packages. |
| 135 | +For example, `cargo build -p foo` will build only `foo` package. |
| 136 | + |
| 137 | +[workspace-section]: ../reference/workspaces.md#the-workspace-section |
| 138 | +[root package]: ../reference/workspaces.md#root-package |
| 139 | +[virtual manifest]: ../reference/workspaces.md#virtual-workspace |
| 140 | +[def-workspace]: ../appendix/glossary.md#workspace '"workspace" (glossary entry)' |
| 141 | +[resolver]: ../reference/resolver.md |
0 commit comments