Skip to content

Commit 4e4d429

Browse files
committed
Add create workspace doc
1 parent 2f17770 commit 4e4d429

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

src/doc/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* [Package Layout](guide/project-layout.md)
1515
* [Cargo.toml vs Cargo.lock](guide/cargo-toml-vs-cargo-lock.md)
1616
* [Tests](guide/tests.md)
17+
* [Creating a Workspace](guide/creating-a-new-workspace.md)
1718
* [Continuous Integration](guide/continuous-integration.md)
1819
* [Cargo Home](guide/cargo-home.md)
1920
* [Build Cache](guide/build-cache.md)
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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

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)