From ecb26299f09cdd118053bff2f26517c6408870ab Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Thu, 11 Jun 2020 14:56:43 -0400 Subject: [PATCH] Added functions for chapter 4 --- src/VariableBindings.rs | 24 +++++++++++++++++++ src/VariableBindings/DeclareFirst.rs | 23 ++++++++++++++++++ src/VariableBindings/Freezing.rs | 17 +++++++++++++ src/VariableBindings/Mutability.rs | 15 ++++++++++++ src/VariableBindings/ScopeAndShadowing.rs | 29 +++++++++++++++++++++++ src/main.rs | 12 ++++++++++ 6 files changed, 120 insertions(+) create mode 100644 src/VariableBindings.rs create mode 100644 src/VariableBindings/DeclareFirst.rs create mode 100644 src/VariableBindings/Freezing.rs create mode 100644 src/VariableBindings/Mutability.rs create mode 100644 src/VariableBindings/ScopeAndShadowing.rs diff --git a/src/VariableBindings.rs b/src/VariableBindings.rs new file mode 100644 index 0000000..742c5b8 --- /dev/null +++ b/src/VariableBindings.rs @@ -0,0 +1,24 @@ +pub mod Mutability; +pub mod ScopeAndShadowing; +pub mod DeclareFirst; +pub mod Freezing; + + +pub fn main(){ + let an_integer = 1u32; + let a_boolean = true; + let unit = (); + + //copy `an_integer` into `copied_integer` + let copied_integer = an_integer; + + println!("An integer: {:?}", copied_integer); + println!("A boolean: {:?}", a_boolean); + println!("Meet the unit value: {:?}", unit); + + //The compiler warns about unused variable bindings; these warnings can be silenced by prefixing the variable name with an underscore + let _unused_variable = 3u32; + + //let noisy_unused_variable = 2u32; + //FIXME ^ Prefix with an underscore to suppress the warning +} diff --git a/src/VariableBindings/DeclareFirst.rs b/src/VariableBindings/DeclareFirst.rs new file mode 100644 index 0000000..b0ffffb --- /dev/null +++ b/src/VariableBindings/DeclareFirst.rs @@ -0,0 +1,23 @@ +pub fn main(){ + // Declare a variable binding + let a_binding; + + { + let x = 2; + + // Initialize the binding + a_binding = x * x; + } + + println!("a binding: {}", a_binding); + + let another_binding; + + // Error! Use of uninitialized binding + //println!("another binding: {}", another_binding); + // FIXME ^ Comment out this line + + another_binding = 1; + + println!("another binding: {}", another_binding); +} diff --git a/src/VariableBindings/Freezing.rs b/src/VariableBindings/Freezing.rs new file mode 100644 index 0000000..7db2530 --- /dev/null +++ b/src/VariableBindings/Freezing.rs @@ -0,0 +1,17 @@ +pub fn main(){ + let mut _mutable_integer = 7i32; + + { + // Shadowing by immutable `_mutable_integer` + let _mutable_integer = _mutable_integer; + + // Error! `_mutable_integer` is frozen in this scope + //_mutable_integer = 50; + // FIXME ^ Comment out this line + + // `_mutable_integer` goes out of scope + } + + // Ok! `_mutable_integer` is not frozen in this scope + _mutable_integer = 3; +} diff --git a/src/VariableBindings/Mutability.rs b/src/VariableBindings/Mutability.rs new file mode 100644 index 0000000..b38eb61 --- /dev/null +++ b/src/VariableBindings/Mutability.rs @@ -0,0 +1,15 @@ +pub fn main(){ + let _immutable_binding = 1; + let mut mutable_binding = 1; + + println!("Before mutation: {}", mutable_binding); + + //Ok + mutable_binding += 1; + + println!("After mutation: {}", mutable_binding); + + //Error! + //_immutable_binding += 1; + //FIXME ^ Comment out this line +} diff --git a/src/VariableBindings/ScopeAndShadowing.rs b/src/VariableBindings/ScopeAndShadowing.rs new file mode 100644 index 0000000..f683d80 --- /dev/null +++ b/src/VariableBindings/ScopeAndShadowing.rs @@ -0,0 +1,29 @@ +pub fn main(){ + // This binding lives in the main function + let long_lived_binding = 1; + + // This is a block, and has a smaller scope than the main function + { + // This binding only exists in this block + let short_lived_binding = 2; + + println!("inner short: {}", short_lived_binding); + + // This binding *shadows* the outer one + let long_lived_binding = 5_f32; + + println!("inner long: {}", long_lived_binding); + } + // End of the block + + // Error! `short_lived_binding` doesn't exist in this scope + //println!("outer short: {}", short_lived_binding); + // FIXME ^ Comment out this line + + println!("outer long: {}", long_lived_binding); + + // This binding also *shadows* the previous binding + let long_lived_binding = 'a'; + + println!("outer long: {}", long_lived_binding); +} diff --git a/src/main.rs b/src/main.rs index c4ade36..b528f92 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ mod HelloWorld; mod Primitives; mod CustomTypes; +mod VariableBindings; fn main(){ @@ -42,4 +43,15 @@ fn main(){ //CustomTypes::Enums::TestCaseLinkedList::main(); //3.3 Constants //CustomTypes::Constants::main(); + + //4 Variable Bindings + VariableBindings::main(); + //4.1 Mutability + VariableBindings::Mutability::main(); + //4.2 Scope and Shadowing + VariableBindings::ScopeAndShadowing::main(); + //4.3 Declare First + VariableBindings::DeclareFirst::main(); + //4.4 Freezing + VariableBindings::Freezing::main(); }