diff --git a/ChangeLog.md b/ChangeLog.md index 3482e37..562bff7 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,5 +1,9 @@ # ChangeLog +### 2022-03-14 + +- Add [Package and Crate](https://practice.rs/crate-module/crate.html) + ### 2022-03-11 - Add one exercise in [Patterns](https://practice.rs/pattern-match/patterns.html) diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 62baa6d..7bb56fa 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -1,6 +1,6 @@ # Summary -- [Rust By Practice](about.md) +- [Rust By Practice](why-exercise.md) - [Variables](variables.md) - [Basic Types](basic-types/intro.md) - [Numbers](basic-types/numbers.md) @@ -39,8 +39,8 @@ - [Result and panic](result-panic/intro.md) - [panic!](result-panic/panic.md) - [Result and ?](result-panic/result.md) -- [Crate and module TODO](crate-module/intro.md) - - [Crate](crate-module/crate.md) +- [Crate and module](crate-module/intro.md) + - [Package and Crate](crate-module/crate.md) - [Module](crate-module/module.md) - [use and pub](crate-module/use-pub.md) - [Comments and Docs TODO](comments-docs.md) diff --git a/src/crate-module/crate.md b/src/crate-module/crate.md index e66fb06..d9084a1 100644 --- a/src/crate-module/crate.md +++ b/src/crate-module/crate.md @@ -1 +1,106 @@ -# Crate +# 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: +```shell +. +โ”œโ”€โ”€ Cargo.toml +โ””โ”€โ”€ src + โ””โ”€โ”€ main.rs + +1 directory, 2 files +``` + +```toml +# in Cargo.toml +[package] +name = "hello-package" +version = "0.1.0" +edition = "2021" +``` + +> Note! We will this package across the whole chapter + +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. ๐ŸŒŸ +```rust +/* 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. + +4. ๐ŸŒŸ +```rust +/* FILL in the blank with your ANSWER */ + +// Q: Whats the name of the library crate in package `hello-package1`? +// A: __ +``` + + +5. ๐ŸŒŸ๐ŸŒŸ Add a library crate for `hello-package` and describe it's files tree below: +```shell +# 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**. + +```shell +# 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. \ No newline at end of file diff --git a/src/crate-module/intro.md b/src/crate-module/intro.md index dadbf20..fac1697 100644 --- a/src/crate-module/intro.md +++ b/src/crate-module/intro.md @@ -1 +1,5 @@ # Crate and module +Learning resources: +- English: [Rust Book Chapter 7](https://doc.rust-lang.org/book/ch07-00-managing-growing-projects-with-packages-crates-and-modules.html) +- ็ฎ€ไฝ“ไธญๆ–‡: [Rust่ฏญ่จ€ๅœฃ็ป - ๅŒ…ๅ’Œๆจกๅ—](https://course.rs/basic/crate-module/intro.html) +