Skip to content

Commit 000267d

Browse files
committed
Adjust chapter content to be guidance rather than practice.
1 parent 55a3818 commit 000267d

File tree

1 file changed

+85
-38
lines changed

1 file changed

+85
-38
lines changed

src/doc/src/guide/creating-a-new-workspace.md

Lines changed: 85 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,83 @@
33
A [workspace][def-workspace] is a collection of one or more packages,
44
called workspace members, that are managed together.
55

6-
In this chapter we implement the creation of a workspace `new_workspace` from scratch,
7-
in which two members `foo-bin` and `bar-lib` are included.
6+
In this chapter, we will create a workspace `new_workspace` containing
7+
binary member `foo` and library member `bar`.
88

99
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-
Here we recommend setting the root of the workspace to be a virtual manifest.
12-
The reason is as follows:
10+
have at least one member, either the [root package] or a [virtual manifest].
1311

14-
- The crate namespace at the Cargo level is flat. The tree layout creates
15-
another hierarchy and increases the possibility of inconsistencies.
16-
- With a flat structure, adding or splitting crates is very easy.
17-
- Even larger flattened lists are easier to see at a glance and easier to
18-
maintain than smaller tree structures.
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+
```
1978

2079
Let's move on and create a virtual workspace.
2180

22-
Currently `Cargo` does not provide automatic creation of workspaces using the command line.
23-
You need to create the `Cargo.toml` file under the folder `new_workspace`:
81+
In the another `new_workspace` empty directory, create a new `Cargo.toml` file and
82+
add the `[workspace]` section:
2483

2584
```toml
2685
# [new_workspace]/Cargo.toml
@@ -37,26 +96,15 @@ even if the default resolver version for workspace members is `2`), for example:
3796
resolver = "2"
3897
```
3998

40-
You can then proceed to create workspace members. As mentioned above,
41-
if you have a lot of workspace members, we recommend designing the workspace
42-
with a flat structure. So add all the workspace members in the directory `new_workspace/crates`.
99+
Likewise, you can then use the `cargo new <package>` command to create
100+
binary member `foo` and library member `bar`.
43101

44-
There are several ways to add members to the workspace, we recommend using the
45-
command `cargo new/init`. For example, add the members `foo-bin` and `bar-lib`
46-
to the workspace:
47-
48-
```console
49-
$ cargo new crates/foo-bin
50-
$ cargo new crates/bar-lib --lib
51-
```
52-
53-
Cargo will automatically add members to `Cargo.toml`:
54102

55103
```toml
56104
# [new_workspace]/Cargo.toml
57105
[workspace]
58106
resolver = "2"
59-
members = [ "crates/bar-lib","crates/foo-bin"]
107+
members = [ "bar","foo"]
60108

61109
```
62110

@@ -66,26 +114,25 @@ The package at this point contains the following files:
66114
$ cd new_workspace
67115
$ tree .
68116
.
117+
├── bar
118+
│ ├── Cargo.toml
119+
│ └── src
120+
│ └── lib.rs
69121
├── Cargo.toml
70-
└── crates
71-
├── bar-lib
72-
│ ├── Cargo.toml
73-
│ └── src
74-
│ └── lib.rs
75-
└── foo-bin
76-
├── Cargo.toml
77-
└── src
78-
└── main.rs
79-
80-
5 directories, 5 files
122+
└── foo
123+
├── Cargo.toml
124+
└── src
125+
└── main.rs
126+
127+
4 directories, 5 files
81128
```
82129

83130
Up to this point, we have a workspace with two members.
84131
Whenever you run `cargo build` under the workspace root directory, Cargo builds
85132
all member at once.
86133
Instead of building the entire workspace, you could use the `--package`/`-p` flag
87134
to select certain packages.
88-
For example, `cargo build -p foo-bin` will build only `foo-bin` package.
135+
For example, `cargo build -p foo` will build only `foo` package.
89136

90137
[workspace-section]: ../reference/workspaces.md#the-workspace-section
91138
[root package]: ../reference/workspaces.md#root-package

0 commit comments

Comments
 (0)