Skip to content

Commit dd7f6d8

Browse files
committed
Add Create workspace doc
1 parent b9d913e commit dd7f6d8

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

src/doc/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* [Cargo Guide](guide/index.md)
1010
* [Why Cargo Exists](guide/why-cargo-exists.md)
1111
* [Creating a New Package](guide/creating-a-new-project.md)
12+
* [Creating a Workspace](guide/creating-a-new-workspace.md)
1213
* [Working on an Existing Package](guide/working-on-an-existing-project.md)
1314
* [Dependencies](guide/dependencies.md)
1415
* [Package Layout](guide/project-layout.md)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

src/doc/src/guide/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ develop Rust packages.
55

66
* [Why Cargo Exists](why-cargo-exists.md)
77
* [Creating a New Package](creating-a-new-project.md)
8+
* [Creating a Workspace](creating-a-new-workspace.md)
89
* [Working on an Existing Cargo Package](working-on-an-existing-project.md)
910
* [Dependencies](dependencies.md)
1011
* [Package Layout](project-layout.md)

0 commit comments

Comments
 (0)