|
| 1 | +# Creating a New Workspace |
| 2 | + |
| 3 | +A [workspace][def-workspace] is a collection of one or more packages, called workspace members, that are managed together. |
| 4 | + |
| 5 | +In this chapter we implement the creation of a workspace `new_workspace` from scratch, in which two members `foo-bin` and `bar-lib` are included. |
| 6 | + |
| 7 | +As mentioned in [[workspace] section](../reference/workspaces.md#the-workspace-section), the workspace must have at least one member, either the root package or a virtual manifest. Here we recommend setting the root of the workspace to be a virtual manifest. The reason is as follows: |
| 8 | + |
| 9 | +- The crate namespace at the Cargo level is flat. The tree layout creates another hierarchy and increases the possibility of inconsistencies. |
| 10 | +- With a flat structure, adding or splitting crates is very easy. |
| 11 | +- Even larger flattened lists are easier to see at a glance and easier to maintain than smaller tree structures. |
| 12 | + |
| 13 | +Let's look at how a workspace containing a virtual inventory should be created. |
| 14 | + |
| 15 | +Currently `Cargo` does not provide automatic creation of workspaces using the command line. You need to create the `Cargo.toml` file under the folder `new_workspace`: |
| 16 | + |
| 17 | +```toml |
| 18 | +# [new_workspace]/Cargo.toml |
| 19 | +[workspace] |
| 20 | +``` |
| 21 | + |
| 22 | +If using a virtual workspace, then the version of [resolver] needs to be specified in the table (if not, the default version of resolver for a workspace is `1`, even if the default resolver version for workspace members is `2`), for example: |
| 23 | + |
| 24 | +```toml |
| 25 | +[workspace] |
| 26 | +resolver = "2" |
| 27 | +``` |
| 28 | + |
| 29 | +You can then proceed to create workspace members. As mentioned above, if you have a lot of workspace members, we recommend designing the workspace with a flat structure. So add all the workspace members in the directory `new_workspace/crates`. |
| 30 | + |
| 31 | +There are several ways to add members to the workspace, we recommend using the command `cargo new/init`. For example, add the members `foo-bin` and `bar-lib` to the workspace: |
| 32 | + |
| 33 | +```console |
| 34 | +$ cargo new crates/foo-bin |
| 35 | +$ cargo new crates/bar-lib |
| 36 | +``` |
| 37 | + |
| 38 | +Cargo will automatically add members to `Cargo.toml`: |
| 39 | + |
| 40 | +```toml |
| 41 | +# [new_workspace]/Cargo.toml |
| 42 | +[workspace] |
| 43 | +resolver = "2" |
| 44 | +members = [ "bar-lib","foo-bin"] |
| 45 | + |
| 46 | +[package] |
| 47 | +# ... |
| 48 | +``` |
| 49 | + |
| 50 | +The project at this point contains the following files: |
| 51 | + |
| 52 | +```console |
| 53 | +$ cd new_workspace |
| 54 | +$ tree . |
| 55 | +. |
| 56 | +├── Cargo.toml |
| 57 | +└── crates |
| 58 | + ├── bar-lib |
| 59 | + │ ├── Cargo.toml |
| 60 | + │ └── src |
| 61 | + │ └── main.rs |
| 62 | + └── foo-bin |
| 63 | + ├── Cargo.toml |
| 64 | + └── src |
| 65 | + └── main.rs |
| 66 | + |
| 67 | +5 directories, 5 files |
| 68 | +``` |
| 69 | + |
| 70 | +Up to this point, we have achieved creating a workspace that contains multiple `crate` members through a step-by-step process. |
| 71 | + |
| 72 | +[def-workspace]: ../appendix/glossary.md#workspace '"workspace" (glossary entry)' |
| 73 | +[resolver]: ../reference/resolver.md |
0 commit comments