| 1429 | } |
| 1430 | |
| 1431 | fn modules(){ |
| 1432 | // Rust provides a powerful module system that can be used to hierarchically split |
| 1433 | // code in logical units (modules), and manage visibility (public/private) between them. |
| 1434 | |
| 1435 | // A module is a collection of items: functions, structs, traits, impl blocks, and even |
| 1436 | // other modules |
| 1437 | |
| 1438 | println!("\n======================================================================"); |
| 1439 | println!("* MODULES"); |
| 1440 | |
| 1441 | // By default, the items in a module have private visibility, but this can be overridden with the pub modifier. |
| 1442 | // Only the public items of a module can be accessed from outside the module scope. |
| 1443 | mod my_mod { |
| 1444 | #[allow(dead_code)] |
| 1445 | fn private_function() { } // Items in modules default to private visibility. |
| 1446 | |
| 1447 | #[allow(dead_code)] |
| 1448 | pub fn function() {} // Use the `pub` modifier to override default visibility. |
| 1449 | |
| 1450 | #[allow(dead_code)] |
| 1451 | pub(crate) fn pub_in_crate() {} // pub(crate) makes functions visible only within the current crate |
| 1452 | // -------------------------------------------------- |
| 1453 | |
| 1454 | // Modules can also be nested |
| 1455 | pub mod nested { |
| 1456 | // Functions declared using `pub(in path)` syntax are only visible |
| 1457 | // within the given path. `path` must be a parent or ancestor module |
| 1458 | // #[allow(dead_code)] |
| 1459 | // pub(in crate::my_mod) fn public_function_in_my_mod() {} |
| 1460 | |
| 1461 | // Functions declared using `pub(self)` syntax are only visible within |
| 1462 | // the current module, which is the same as leaving them private |
| 1463 | #[allow(dead_code)] |
| 1464 | pub(self) fn public_function_in_nested() {} |
| 1465 | |
| 1466 | // Functions declared using `pub(super)` syntax are only visible within |
| 1467 | // the parent module |
| 1468 | #[allow(dead_code)] |
| 1469 | pub(super) fn public_function_in_super_mod() {} |
| 1470 | } |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | fn crates(){ |
| 1475 | // A crate is a compilation unit in Rust. Whenever rustc some_file.rs |