chapter 7 Modules
This commit is contained in:
parent
d047b26213
commit
4e4ec625e0
71
src/main.rs
71
src/main.rs
@ -8,10 +8,12 @@ fn main() {
|
|||||||
chapter_4();
|
chapter_4();
|
||||||
chapter_5();
|
chapter_5();
|
||||||
chapter_6();
|
chapter_6();
|
||||||
|
chapter_7();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn chapter_1() {
|
fn chapter_1() {
|
||||||
println!("Chapter 1: Getting Started");
|
println!("Chapter 1: Getting Started");
|
||||||
|
println!("`rustup update` to update rust.");
|
||||||
println!("
|
println!("
|
||||||
The compiler is named `rustc`, the package manager is `cargo`.
|
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.
|
Using cargo provide a lot of ease in setting up and building a rust project.
|
||||||
@ -375,7 +377,7 @@ fn first_word(s: &String) -> &str {
|
|||||||
|
|
||||||
for (i, &e) in bytes.iter().enumerate() {
|
for (i, &e) in bytes.iter().enumerate() {
|
||||||
if e == b' ' {
|
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() {
|
for (i, &e) in bytes.iter().enumerate() {
|
||||||
if e == b' ' {
|
if e == b' ' {
|
||||||
return &s[0..i]
|
return &s[0..i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -566,9 +568,12 @@ fn method_syntax() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn chapter_6() {
|
fn chapter_6() {
|
||||||
|
println!("Chapter 6: Enums and pattern matching");
|
||||||
defining_enum();
|
defining_enum();
|
||||||
match_control_flow();
|
match_control_flow();
|
||||||
concise_control_flow_if_let();
|
concise_control_flow_if_let();
|
||||||
|
|
||||||
|
println!();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
@ -669,13 +674,13 @@ fn match_control_flow() {
|
|||||||
UsCoin::Penny => {
|
UsCoin::Penny => {
|
||||||
println!("Lucky penny!");
|
println!("Lucky penny!");
|
||||||
1
|
1
|
||||||
},
|
}
|
||||||
UsCoin::Nickel => 5,
|
UsCoin::Nickel => 5,
|
||||||
UsCoin::Dime => 10,
|
UsCoin::Dime => 10,
|
||||||
UsCoin::Quarter(state) => {
|
UsCoin::Quarter(state) => {
|
||||||
println!("State quarter from {:?}!", state);
|
println!("State quarter from {:?}!", state);
|
||||||
25
|
25
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -762,3 +767,61 @@ fn concise_control_flow_if_let() {
|
|||||||
|
|
||||||
println!();
|
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},
|
||||||
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user