From 4e4ec625e02205e855ac78994f93d62bb2f473ca Mon Sep 17 00:00:00 2001 From: Zykino Date: Sat, 27 Oct 2018 14:50:06 +0200 Subject: [PATCH] chapter 7 Modules --- src/main.rs | 79 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 71 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6c97586..3047a9e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,10 +8,12 @@ fn main() { chapter_4(); chapter_5(); chapter_6(); + chapter_7(); } fn chapter_1() { println!("Chapter 1: Getting Started"); + println!("`rustup update` to update rust."); println!(" The compiler is named `rustc`, the package manager is `cargo`. Using cargo provide a lot of ease in setting up and building a rust project. @@ -156,7 +158,7 @@ fn control_flow() { let divisor = if number % 4 == 0 { "Four" - } else if number % 3 == 0{ + } else if number % 3 == 0 { "Three" } else if number % 2 == 0 { "Two" @@ -375,7 +377,7 @@ fn first_word(s: &String) -> &str { for (i, &e) in bytes.iter().enumerate() { if e == b' ' { - return &s[0..i] + return &s[0..i]; } } @@ -389,7 +391,7 @@ fn first_word_updated(s: &str) -> &str { for (i, &e) in bytes.iter().enumerate() { if e == b' ' { - return &s[0..i] + return &s[0..i]; } } @@ -454,8 +456,8 @@ fn structs() { }; println!("We can create a struct from an previous one to have it's values as default. {}", user4.active); - struct Color (i32, i32, i32); - struct Point (i32, i32, i32); + struct Color(i32, i32, i32); + struct Point(i32, i32, i32); let black = Color(0, 0, 0); let _origin = Point(0, 0, 0); @@ -566,9 +568,12 @@ fn method_syntax() { } fn chapter_6() { + println!("Chapter 6: Enums and pattern matching"); defining_enum(); match_control_flow(); concise_control_flow_if_let(); + + println!(); } #[allow(dead_code)] @@ -621,7 +626,7 @@ fn defining_enum() { println!("NOTE: The standard library implements an IpAddr enum, better use it than our custom one 😉."); impl IpAddr { - fn route(&self) { + fn route(&self) { // Method would be defined here unimplemented!() } @@ -669,13 +674,13 @@ fn match_control_flow() { UsCoin::Penny => { println!("Lucky penny!"); 1 - }, + } UsCoin::Nickel => 5, UsCoin::Dime => 10, UsCoin::Quarter(state) => { println!("State quarter from {:?}!", state); 25 - }, + } } } @@ -762,3 +767,61 @@ fn concise_control_flow_if_let() { println!(); } + +fn chapter_7() { + println!("Chapter 7: Modules"); + mod_and_the_filesystem(); + controlling_visibility_with_pub(); + referring_to_names_in_differents_modules(); + + println!(); +} + +fn mod_and_the_filesystem() { + println!("Modules creates namespaces. We can access module's functions with `::`."); + println!("We can nest modules. Modules can have the same names and functions if they don't have the same hierarchy."); + println!("Modules can be implementerd inline or declared and the implementation is deported in an external file."); + println!("The external file should be named with the name of the module: `mod toto;` => toto.rs"); + println!("If the module has sub-modules, instead of a file we create a folder named exactly like the module with a mod.rs file in it."); + println!("This structure may be recursive."); + + println!(); +} + +fn controlling_visibility_with_pub() { + println!("By default, modules and functions are privates. We use the `pub` keyword to make them public."); + println!("If an item is public, it can be accessed though any of its parent modules."); + println!("If an item is private, it can be accessed only by its immediate parent module and any of the parent's child modules."); + + println!(); +} + +fn referring_to_names_in_differents_modules() { + println!("The `use` keyword bring a module, a function or an enum variant into the scope."); + println!("We can bring multiple item on the scope with a curly bracket list, or all of the item with the `*` glob operator."); + // See below for a complex / coplete example. + println!("We can reference an element with an absolute path: `::client::connect()`."); + println!("We can reference an element with an relative path to the parent: `super::client::connect()`."); + + println!(); +} + +#[allow(unused_imports, dead_code)] +// Preparation +mod foo { + pub mod bar { + pub type Foo = (); + } + pub mod baz { + pub mod quux { + pub type Bar = (); + } + } +} + +#[allow(unused_imports, dead_code)] +// Importing into scope +use foo::{ + bar::{self, Foo}, + baz::{*, quux::Bar}, +};