rust-by-practice/en/src/crate-module/crate.md

2.9 KiB

Package and Crate

A package is a project which you create with Cargo (most cases), so it contains a Cargo.toml file in it.

  1. 🌟 Create a package with below layout:
.
β”œβ”€β”€ Cargo.toml
└── src
    └── main.rs

1 directory, 2 files
# in Cargo.toml
[package]
name = "hello-package"
version = "0.1.0"
edition = "2021"

Note! We will use this package across the whole chapter as a practice project.

  1. 🌟 Create a package with below layout:
.
β”œβ”€β”€ Cargo.toml
└── src
    └── lib.rs

1 directory, 2 files
# 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.

  1. 🌟
/* FILL in the blank with your ANSWER */

// Q: Whats the difference between package 1# and 2# ?
// 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.

  1. 🌟
/* FILL in the blank with your ANSWER */

// Q: Whats the name of the library crate in package `hello-package1`?
// A: __
  1. 🌟🌟 Add a library crate for hello-package and describe it's files tree below:
# 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.

  1. 🌟🌟🌟 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.
# 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
β”‚   β”œβ”€β”€ __
β”‚   β”œβ”€β”€ __
β”‚   └── __
β”‚       └── __
β”‚       └── __
β”œβ”€β”€ 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

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 (under the solutions path), but only use it when you need it :)