mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 12:39:42 +00:00
Add [Package and Crate]
This commit is contained in:
@ -1,5 +1,9 @@
|
|||||||
# ChangeLog
|
# ChangeLog
|
||||||
|
|
||||||
|
### 2022-03-14
|
||||||
|
|
||||||
|
- Add [Package and Crate](https://practice.rs/crate-module/crate.html)
|
||||||
|
|
||||||
### 2022-03-11
|
### 2022-03-11
|
||||||
|
|
||||||
- Add one exercise in [Patterns](https://practice.rs/pattern-match/patterns.html)
|
- Add one exercise in [Patterns](https://practice.rs/pattern-match/patterns.html)
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
# Summary
|
# Summary
|
||||||
|
|
||||||
- [Rust By Practice](about.md)
|
- [Rust By Practice](why-exercise.md)
|
||||||
- [Variables](variables.md)
|
- [Variables](variables.md)
|
||||||
- [Basic Types](basic-types/intro.md)
|
- [Basic Types](basic-types/intro.md)
|
||||||
- [Numbers](basic-types/numbers.md)
|
- [Numbers](basic-types/numbers.md)
|
||||||
@ -39,8 +39,8 @@
|
|||||||
- [Result and panic](result-panic/intro.md)
|
- [Result and panic](result-panic/intro.md)
|
||||||
- [panic!](result-panic/panic.md)
|
- [panic!](result-panic/panic.md)
|
||||||
- [Result and ?](result-panic/result.md)
|
- [Result and ?](result-panic/result.md)
|
||||||
- [Crate and module TODO](crate-module/intro.md)
|
- [Crate and module](crate-module/intro.md)
|
||||||
- [Crate](crate-module/crate.md)
|
- [Package and Crate](crate-module/crate.md)
|
||||||
- [Module](crate-module/module.md)
|
- [Module](crate-module/module.md)
|
||||||
- [use and pub](crate-module/use-pub.md)
|
- [use and pub](crate-module/use-pub.md)
|
||||||
- [Comments and Docs TODO](comments-docs.md)
|
- [Comments and Docs TODO](comments-docs.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.
|
@ -1 +1,5 @@
|
|||||||
# Crate and module
|
# 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)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user