mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-08-12 06:24:44 +00:00
update repo layout
This commit is contained in:
109
en/src/crate-module/crate.md
Normal file
109
en/src/crate-module/crate.md
Normal file
@@ -0,0 +1,109 @@
|
||||
# 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 use this package across the whole chapter as a practice project.
|
||||
|
||||
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,editable
|
||||
/* 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,editable
|
||||
/* 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,editable
|
||||
# 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,editable
|
||||
# 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](https://github.com/sunface/rust-by-practice) (under the solutions path), but only use it when you need it :)
|
5
en/src/crate-module/intro.md
Normal file
5
en/src/crate-module/intro.md
Normal file
@@ -0,0 +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)
|
||||
|
195
en/src/crate-module/module.md
Normal file
195
en/src/crate-module/module.md
Normal file
@@ -0,0 +1,195 @@
|
||||
# Module
|
||||
Modules let us organize the code within a crate into groups by readablity and easy reuse. Module also controls the privacy of items, which is whether an item can be seen by outside code( public ), or is just an internal implementation and not available for outside code( private ).
|
||||
|
||||
|
||||
We have created a package named `hello-package` in previous chapter, and it looks like this:
|
||||
```shell
|
||||
.
|
||||
├── Cargo.toml
|
||||
├── src
|
||||
│ ├── lib.rs
|
||||
│ └── main.rs
|
||||
```
|
||||
|
||||
Now it's time to create some modules in the library crate and use them in the binary crate, let's start.
|
||||
|
||||
1. 🌟🌟 Implement module `front_of_house` based on the module tree below:
|
||||
```shell
|
||||
library crate root
|
||||
└── front_of_house
|
||||
├── hosting
|
||||
│ ├── add_to_waitlist
|
||||
│ └── seat_at_table
|
||||
└── serving
|
||||
├── take_order
|
||||
├── serve_order
|
||||
├── take_payment
|
||||
└── complain
|
||||
```
|
||||
|
||||
```rust,editable
|
||||
// FILL in the blank
|
||||
// in __.rs
|
||||
|
||||
mod front_of_house {
|
||||
// IMPLEMENT this module..
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
2. 🌟🌟 Let's call `add_to_waitlist` from a function `eat_at_restaurant` which within the library crate root.
|
||||
|
||||
```rust
|
||||
// in lib.rs
|
||||
|
||||
// FILL in the blanks and FIX the errors
|
||||
// You need make something public with `pub` to provide accessiblity for outside code `fn eat_at_restaurant()`
|
||||
mod front_of_house {
|
||||
/* ...snip... */
|
||||
}
|
||||
|
||||
pub fn eat_at_restaurant() {
|
||||
// call add_to_waitlist with **absolute path**:
|
||||
__.add_to_waitlist();
|
||||
|
||||
// call with **relative path**
|
||||
__.add_to_waitlist();
|
||||
}
|
||||
```
|
||||
|
||||
3. 🌟🌟 You can use `super` to import items within the parent module
|
||||
```rust,editable
|
||||
// in lib.rs
|
||||
|
||||
mod back_of_house {
|
||||
fn fix_incorrect_order() {
|
||||
cook_order();
|
||||
// FILL in the blank in tree ways
|
||||
//1. using keyword `super`
|
||||
//2. using absolute path
|
||||
__.serve_order();
|
||||
}
|
||||
|
||||
fn cook_order() {}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### Separating modules into different files
|
||||
```rust
|
||||
// in lib.rs
|
||||
pub mod front_of_house {
|
||||
pub mod hosting {
|
||||
pub fn add_to_waitlist() {}
|
||||
|
||||
pub fn seat_at_table() -> String {
|
||||
String::from("sit down please")
|
||||
}
|
||||
}
|
||||
|
||||
pub mod serving {
|
||||
pub fn take_order() {}
|
||||
|
||||
pub fn serve_order() {}
|
||||
|
||||
pub fn take_payment() {}
|
||||
|
||||
// Maybe you don't want the guest hearing the your complaining about them
|
||||
// So just make it private
|
||||
fn complain() {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eat_at_restaurant() -> String {
|
||||
front_of_house::hosting::add_to_waitlist();
|
||||
|
||||
back_of_house::cook_order();
|
||||
|
||||
String::from("yummy yummy!")
|
||||
}
|
||||
|
||||
pub mod back_of_house {
|
||||
pub fn fix_incorrect_order() {
|
||||
cook_order();
|
||||
crate::front_of_house::serving::serve_order();
|
||||
}
|
||||
|
||||
pub fn cook_order() {}
|
||||
}
|
||||
```
|
||||
|
||||
4. 🌟🌟🌟🌟 Please separate the modules and codes above into files resident in below dir tree :
|
||||
```shell
|
||||
.
|
||||
├── Cargo.toml
|
||||
├── src
|
||||
│ ├── back_of_house.rs
|
||||
│ ├── front_of_house
|
||||
│ │ ├── hosting.rs
|
||||
│ │ ├── mod.rs
|
||||
│ │ └── serving.rs
|
||||
│ ├── lib.rs
|
||||
│ └── main.rs
|
||||
```
|
||||
|
||||
```rust,editable
|
||||
// in src/lib.rs
|
||||
|
||||
// IMPLEMENT...
|
||||
```
|
||||
|
||||
```rust,editable
|
||||
// in src/back_of_house.rs
|
||||
|
||||
// IMPLEMENT...
|
||||
```
|
||||
|
||||
|
||||
```rust,editable
|
||||
// in src/front_of_house/mod.rs
|
||||
|
||||
// IMPLEMENT...
|
||||
```
|
||||
|
||||
```rust,editable
|
||||
// in src/front_of_house/hosting.rs
|
||||
|
||||
// IMPLEMENT...
|
||||
```
|
||||
|
||||
```rust,editable
|
||||
// in src/front_of_house/serving.rs
|
||||
|
||||
// IMPLEMENT...
|
||||
```
|
||||
|
||||
### accessing code in library crate from binary crate
|
||||
**Please ensure you have completed the 4th exercise before making further progress.**
|
||||
|
||||
You should have below structures and the corresponding codes in them when reaching here:
|
||||
```shell
|
||||
.
|
||||
├── Cargo.toml
|
||||
├── src
|
||||
│ ├── back_of_house.rs
|
||||
│ ├── front_of_house
|
||||
│ │ ├── hosting.rs
|
||||
│ │ ├── mod.rs
|
||||
│ │ └── serving.rs
|
||||
│ ├── lib.rs
|
||||
│ └── main.rs
|
||||
```
|
||||
|
||||
5. 🌟🌟🌟 Now we will call a few library functions from the binary crate.
|
||||
|
||||
```rust,editable
|
||||
// in src/main.rs
|
||||
|
||||
// FILL in the blank and FIX the errors
|
||||
fn main() {
|
||||
assert_eq!(__, "sit down please");
|
||||
assert_eq!(__,"yummy yummy!");
|
||||
}
|
||||
```
|
||||
|
||||
> 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 :)
|
69
en/src/crate-module/use-pub.md
Normal file
69
en/src/crate-module/use-pub.md
Normal file
@@ -0,0 +1,69 @@
|
||||
# use and pub
|
||||
1. 🌟 We can bring two types of the same name into the same scope with use, but you need `as` keyword.
|
||||
|
||||
```rust,editable
|
||||
use std::fmt::Result;
|
||||
use std::io::Result;
|
||||
|
||||
fn main() {}
|
||||
```
|
||||
|
||||
2. 🌟🌟 If we are using multiple items defined in the same crate or module, then listing each item on its own line will take up too much verticall space.
|
||||
|
||||
```rust,editable
|
||||
|
||||
// FILL in the blank in two ways
|
||||
// DON'T add new code line
|
||||
use std::collections::__;
|
||||
|
||||
fn main() {
|
||||
let _c1:HashMap<&str, i32> = HashMap::new();
|
||||
let mut c2 = BTreeMap::new();
|
||||
c2.insert(1, "a");
|
||||
let _c3: HashSet<i32> = HashSet::new();
|
||||
}
|
||||
```
|
||||
|
||||
### Re-exporting names with `pub use`
|
||||
3. 🌟🌟🌟 In our recently created package `hello-package`, add something to make the below code work
|
||||
```rust,editable
|
||||
fn main() {
|
||||
assert_eq!(hello_package::hosting::seat_at_table(), "sit down please");
|
||||
assert_eq!(hello_package::eat_at_restaurant(),"yummy yummy!");
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### pub(in Crate)
|
||||
Sometimes we want an item only be public to a certain crate, then we can use the `pub(in Crate)` syntax.
|
||||
|
||||
#### Example
|
||||
```rust,editable
|
||||
pub mod a {
|
||||
pub const I: i32 = 3;
|
||||
|
||||
fn semisecret(x: i32) -> i32 {
|
||||
use self::b::c::J;
|
||||
x + J
|
||||
}
|
||||
|
||||
pub fn bar(z: i32) -> i32 {
|
||||
semisecret(I) * z
|
||||
}
|
||||
pub fn foo(y: i32) -> i32 {
|
||||
semisecret(I) + y
|
||||
}
|
||||
|
||||
mod b {
|
||||
pub(in crate::a) mod c {
|
||||
pub(in crate::a) const J: i32 = 4;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Full Code
|
||||
The full code of `hello-package` is [here](https://github.com/sunface/rust-by-practice/tree/master/practices/hello-package).
|
||||
|
||||
|
||||
> 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 :)
|
Reference in New Issue
Block a user