2022-03-14 01:39:22 -06:00
# Package and Crate
2022-08-22 12:50:45 -05:00
A package is a project which you create with Cargo (in most cases), so it contains a `Cargo.toml` file in it.
2022-03-14 01:39:22 -06:00
1. π Create a package with below layout:
```shell
.
βββ Cargo.toml
βββ src
βββ main.rs
1 directory, 2 files
```
```toml
# in Cargo.toml
[package]
name = "hello-package"
version = "0.1.0"
edition = "2021"
```
2022-03-14 19:36:42 -06:00
> Note! We will use this package across the whole chapter as a practice project.
2022-03-14 01:39:22 -06:00
2. π Create a package with below layout:
```shell
.
βββ Cargo.toml
βββ src
βββ lib.rs
1 directory, 2 files
```
```toml
# in Cargo.toml
[package]
name = "hello-package1"
version = "0.1.0"
edition = "2021"
```
> Note! This package could be safely removed due to the first one's existence.
3. π
2022-03-14 02:18:28 -06:00
```rust,editable
2022-03-14 01:39:22 -06:00
/* FILL in the blank with your ANSWER */
2022-08-22 12:50:45 -05:00
// Q: What's the difference between package number 1 and number 2?
2022-03-14 01:39:22 -06:00
// A: __
```
## Crate
A crate is a binary or library. The crate root is a source file that the Rust compiler starts from and makes up the root module of the crate.
In package `hello-package` , there is binary crate with the same name as the package : `hello-package` , and `src/main.rs` is the crate root of this binary crate.
Similar to `hello-package` , `hello-package1` also has a crate in it, however, this package doesn't contain a binary crate but a library crate, and `src/lib.rs` is the crate root.
4. π
2022-03-14 02:18:28 -06:00
```rust,editable
2022-03-14 01:39:22 -06:00
/* FILL in the blank with your ANSWER */
2022-08-22 12:50:45 -05:00
// Q: What's the name of the library crate in package `hello-package1` ?
2022-03-14 01:39:22 -06:00
// A: __
```
5. ππ Add a library crate for `hello-package` and describe it's files tree below:
2022-03-14 02:18:28 -06:00
```shell,editable
2022-03-14 01:39:22 -06:00
# FILL in the blanks
.
βββ Cargo.lock
βββ Cargo.toml
βββ src
βΒ Β βββ __
βΒ Β βββ __
```
After this step, there should be two crates in package `hello-package` : **a binary crate and a library crate, both with the same name as the package** .
6. πππ A package can contain at most one library crate, but it can contain as many binary crates as you would like by placing files in `src/bin` directory: **each file will be a separate binary crate with the same name as the file** .
2022-03-14 02:18:28 -06:00
```shell,editable
2022-03-14 01:39:22 -06:00
# Create a package which contains
# 1. three binary crates: `hello-package`, `main1` and `main2`
# 2. one library crate
# describe the directory tree below
.
βββ Cargo.toml
βββ Cargo.lock
βββ src
2022-03-14 19:36:42 -06:00
β βββ __
β βββ __
β βββ __
β βββ __
β βββ __
2022-03-14 01:39:22 -06:00
βββ tests # directory for integrated tests files
β βββ some_integration_tests.rs
βββ benches # dir for benchmark files
β βββ simple_bench.rs
βββ examples # dir for example files
βββ simple_example.rs
```
2022-03-14 19:36:42 -06:00
Yep, as you can see, the above package structure is very standard and is widely used in many Rust projects.
> You can find the solutions [here](https://github.com/sunface/rust-by-practice) (under the solutions path), but only use it when you need it :)