Merge pull request #303 from Tanish-Eagle/edit

Edit
This commit is contained in:
Sunface 2022-09-09 10:05:17 +08:00 committed by GitHub
commit 74823e30e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 23 deletions

View File

@ -1,5 +1,5 @@
# Package and Crate
A package is a project which you create with Cargo (most cases), so it contains a `Cargo.toml` file in it.
A package is a project which you create with Cargo (in most cases), so it contains a `Cargo.toml` file in it.
1. 🌟 Create a package with below layout:
```shell
@ -45,7 +45,7 @@ edition = "2021"
```rust,editable
/* FILL in the blank with your ANSWER */
// Q: Whats the difference between package 1# and 2# ?
// Q: What's the difference between package number 1 and number 2?
// A: __
```
@ -61,7 +61,7 @@ Similar to `hello-package`, `hello-package1` also has a crate in it, however, th
```rust,editable
/* FILL in the blank with your ANSWER */
// Q: Whats the name of the library crate in package `hello-package1`?
// Q: What's the name of the library crate in package `hello-package1`?
// A: __
```

View File

@ -1,5 +1,5 @@
# 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 ).
Modules let us organize the code within a crate into groups for readability and ease of 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:
@ -37,34 +37,34 @@ mod front_of_house {
```
2. 🌟🌟 Let's call `add_to_waitlist` from a function `eat_at_restaurant` which within the library crate root.
2. 🌟🌟 Let's call `add_to_waitlist` from a function `eat_at_restaurant` which is within the library crate root.
```rust,editable
// in lib.rs
// 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()`
// You need to make something public with `pub` to provide accessibility for outside code `fn eat_at_restaurant()`
mod front_of_house {
/* ...snip... */
}
pub fn eat_at_restaurant() {
// call add_to_waitlist with **absolute path**:
// Call add_to_waitlist with **absolute path**:
__.add_to_waitlist();
// call with **relative path**
// Call with **relative path**
__.add_to_waitlist();
}
```
3. 🌟🌟 You can use `super` to import items within the parent module
```rust,editable
// in lib.rs
// In lib.rs
mod back_of_house {
fn fix_incorrect_order() {
cook_order();
// FILL in the blank in tree ways
// FILL in the blank in three ways
//1. using keyword `super`
//2. using absolute path
__.serve_order();
@ -77,7 +77,7 @@ mod back_of_house {
### Separating modules into different files
```rust,editable
// in lib.rs
// In lib.rs
pub mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
@ -133,37 +133,37 @@ pub mod back_of_house {
```
```rust,editable
// in src/lib.rs
// In src/lib.rs
// IMPLEMENT...
```
```rust,editable
// in src/back_of_house.rs
// In src/back_of_house.rs
// IMPLEMENT...
```
```rust,editable
// in src/front_of_house/mod.rs
// In src/front_of_house/mod.rs
// IMPLEMENT...
```
```rust,editable
// in src/front_of_house/hosting.rs
// In src/front_of_house/hosting.rs
// IMPLEMENT...
```
```rust,editable
// in src/front_of_house/serving.rs
// In src/front_of_house/serving.rs
// IMPLEMENT...
```
### accessing code in library crate from binary crate
### 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:
@ -183,7 +183,7 @@ You should have below structures and the corresponding codes in them when reachi
5. 🌟🌟🌟 Now we will call a few library functions from the binary crate.
```rust,editable
// in src/main.rs
// In src/main.rs
// FILL in the blank and FIX the errors
fn main() {

View File

@ -1,4 +1,4 @@
# use and pub
# 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
@ -8,7 +8,7 @@ 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.
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 vertical space.
```rust,editable
@ -34,8 +34,8 @@ fn main() {
```
### pub(in Crate)
Sometimes we want an item only be public to a certain crate, then we can use the `pub(in Crate)` syntax.
### Pub(in Crate)
Sometimes we want an item only be public to a certain crate. For this we can use the `pub(in Crate)` syntax.
#### Example
```rust,editable