You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When the code base is getting larger, you might need to work with **multiple crates on the same project**. Rust supports this via Workspaces. You can **analyze (`cargo check`), build, run tests or generate docs for all crates** at once by running `cargo` commands from the project root.
10
-
11
-
βοΈ When working on multiple crates same time, there is a higher possibility of having shared dependencies on crates. To prevent downloading and compiling the same dependency multiple times, Rust uses a **shared build directory** under the project root, while running `cargo build` from the project root.
12
-
13
-
Let's create a library crate with a simple hello world function and a binary crate which uses the library crate.
14
-
15
-
Assume we run,
16
-
```bash
17
-
mkdir greetings
18
-
touch greetings/Cargo.toml
19
-
cargo new greetings/lib --lib
20
-
cargo new greetings/examples/hello
6
+
## Rust Workspaces
7
+
8
+
- Rust workspaces provide a convenient way to manage multiple related crates together as a single project or monorepo.
9
+
- A Rust workspace uses a shared `Cargo.lock` file and a common `target` directory to efficiently build and manage shared dependencies, reducing build times.
10
+
- Cargo commands such as `cargo test`, `cargo build`, `cargo check`, `cargo fmt`, `cargo clippy`, and `cargo clean` can be run once from the workspace root to apply to all workspace members.
11
+
12
+
## Simple
13
+
14
+
Let's build a sample Rust workspace with two simple crates.
// 03.2 greetings/examples/hello/src/main.rs to import the `greetings` lib and call its hello world function
70
-
externcrate greetings;
71
-
72
-
fnmain() {
73
-
greetings::hello();
74
-
}
142
+
common = { path = "../common" }
75
143
```
144
+
{{< /tab >}}
145
+
{{< tab label="my_project2 β Cargo.toml" >}}
146
+
```toml {title="my_project/Cargo.toml"}
147
+
[workspace]
148
+
resolver = "3"
149
+
members = ["crates/*"]
150
+
```
151
+
{{< /tab >}}
152
+
{{< /tabs >}}
76
153
77
-
π‘ On Linux and Mac, you can run `cargo` commands on each crate without changing the working directory all the times via Subshells (A command list embedded between parentheses). For example, if you are in the `greetings` directory, even you run `(cd examples/hello && cargo run)` your working directory will be kept as same in `greetings` folder.
154
+
## π¨βπ« Before going to the next...
78
155
79
-
> [!assignment]
80
-
> [rust-lang/rust library folder](https://github.com/rust-lang/rust/tree/main/library) is a good example for a workspace.
156
+
-[rust-lang/rust library folder](https://github.com/rust-lang/rust/tree/main/library) is a good example for a workspace. Explore and identify Rust library workspace structure.
0 commit comments