diff --git a/content/lessons/03_data_types/index.md b/content/lessons/03_data_types/index.md index d928a52..219b1cc 100644 --- a/content/lessons/03_data_types/index.md +++ b/content/lessons/03_data_types/index.md @@ -107,6 +107,6 @@ So how do we handle situations which can fail? That's where the `Result` type co ## Assignment 2 (graded) -[Communications](https://classroom.github.com/a/K_w_zJJ9) +[Communications](https://classroom.github.com/a/gDraT0lo) Deadline: 23.10.2024 23:59 \ No newline at end of file diff --git a/content/lessons/03_data_types/module_system/module_system.html b/content/lessons/03_data_types/module_system/module_system.html new file mode 100644 index 0000000..d615dd1 --- /dev/null +++ b/content/lessons/03_data_types/module_system/module_system.html @@ -0,0 +1,272 @@ +
+

Module system

+

Managing code structure in a growing project.
+Testing and sharing code conveniently.

+
+
+

Module system consists of:

+
    +
  • Packages: A Cargo feature that lets you build, test, and share crates
  • +
  • Crates: A tree of modules that produces a library or executable
  • +
  • Modules and use: Let you control the organization, scope, and privacy of paths
  • +
  • Paths: A way of naming an item, such as a struct, function, or module
  • +
+
+
+

Package structure

+
my-project
+├── Cargo.lock             <-- actual dependencies' versions
+├── Cargo.toml             <-- package configuration, dependency version requirements
+└── src
+    ├── configuration
+    │   ├── run.rs
+    │   └── mod.rs
+    ├── lib.rs             <-- root of the lib crate
+    ├── bin1
+    │   ├── distribution.rs
+    │   └── main.rs        <-- root of bin crate `bin1`
+    └── bin2.rs            <-- root of bin crate `bin2`
+
+
+
+

Lib crates can be shared

+
    +
  • crates.io is the main crate repository.
  • +
  • If you specify a dependency in Cargo.toml, it's fetched from crates.io automatically by Cargo.
  • +
  • lib.rs is the root of a lib crate.
  • +
+
+
+

Binary crates can be executed

+
    +
  • cargo run executes the bin crate in your package.
  • +
  • If you have multiple bin crates, you have to specify which to run:
    +cargo run --bin <bin_name>
  • +
  • Each bin crate in a package can import code from the lib crate there.
  • +
+
+
+ +
mod front_of_house {
+    mod hosting {
+        fn add_to_waitlist() {}
+        fn seat_at_table() {}
+    }
+    
+    // Alternatively, this could be located in `serving.rs` file and imported.
+    mod serving {
+        fn take_order() {}
+        fn serve_order() {}
+        fn take_payment() {}
+    }
+}
+
+
+
+ +
crate
+ └── front_of_house
+     ├── hosting
+     │   ├── add_to_waitlist
+     │   └── seat_at_table
+     └── serving
+         ├── take_order
+         ├── serve_order
+         └── take_payment
+
+
+
+

Exports & imports

+
    +
  • exports: using privacy modifier (pub, pub(crate), )
  • +
+
mod some_mod {
+    struct ModulePublic;
+    pub(super) struct ParentModulePublic;
+    pub(crate) struct CratePublic;
+    pub struct WorldPublic;
+}
+
+
    +
  • imports: using use statement
  • +
+
use some_mod::CratePublic;
+pub use some_mod::WorldPublic; // <-- re-export
+
+
+
+
\ No newline at end of file diff --git a/content/lessons/03_data_types/module_system/module_system.md b/content/lessons/03_data_types/module_system/module_system.md new file mode 100644 index 0000000..e5541a2 --- /dev/null +++ b/content/lessons/03_data_types/module_system/module_system.md @@ -0,0 +1,121 @@ +--- +marp: true +author: Wojciech Przytuła +backgroundColor: black +color: grey +transition: fade +theme: gaia +style: | + pre { + background-color: black; + border-style: solid; + border-color: grey; + }, + + code { + background-color: black; + } +--- + +# Module system + +Managing code structure in a growing project. +Testing and sharing code conveniently. + +--- + +## Module system consists of: + +- **Packages**: A Cargo feature that lets you build, test, and share crates +- **Crates**: A tree of modules that produces a library or executable +- **Modules and `use`**: Let you control the organization, scope, and privacy of paths +- **Paths**: A way of naming an item, such as a struct, function, or module + +--- + +## Package structure + +``` +my-project +├── Cargo.lock <-- actual dependencies' versions +├── Cargo.toml <-- package configuration, dependency version requirements +└── src + ├── configuration + │ ├── run.rs + │ └── mod.rs + ├── lib.rs <-- root of the lib crate + ├── bin1 + │ ├── distribution.rs + │ └── main.rs <-- root of bin crate `bin1` + └── bin2.rs <-- root of bin crate `bin2` +``` + +--- + +## *Lib crates* can be shared + +- *crates.io* is the main crate repository. +- If you specify a dependency in `Cargo.toml`, it's fetched from `crates.io` automatically by *Cargo*. +- `lib.rs` is the root of a *lib crate*. + +--- + +## *Binary crates* can be executed + +- `cargo run` executes the bin crate in your package. +- If you have multiple bin crates, you have to specify which to run: +`cargo run --bin ` +- Each bin crate in a package can import code from the lib crate there. + +--- + +## Modules: grouping related code (& encapsulation) +```rust +mod front_of_house { + mod hosting { + fn add_to_waitlist() {} + fn seat_at_table() {} + } + + // Alternatively, this could be located in `serving.rs` file and imported. + mod serving { + fn take_order() {} + fn serve_order() {} + fn take_payment() {} + } +} +``` + +--- +## Modules: grouping related code (& encapsulation) + +``` +crate + └── front_of_house + ├── hosting + │ ├── add_to_waitlist + │ └── seat_at_table + └── serving + ├── take_order + ├── serve_order + └── take_payment +``` + +--- + +## Exports & imports + +- exports: using privacy modifier (`pub`, `pub(crate)`, ) +```rust +mod some_mod { + struct ModulePublic; + pub(super) struct ParentModulePublic; + pub(crate) struct CratePublic; + pub struct WorldPublic; +} +``` +- imports: using `use` statement +```rust +use some_mod::CratePublic; +pub use some_mod::WorldPublic; // <-- re-export +``` \ No newline at end of file diff --git a/content/lessons/03_data_types/module_system/module_system.pdf b/content/lessons/03_data_types/module_system/module_system.pdf new file mode 100644 index 0000000..da222e7 Binary files /dev/null and b/content/lessons/03_data_types/module_system/module_system.pdf differ