Added functions for chapter 4

This commit is contained in:
2020-06-11 14:56:43 -04:00
parent 3457bd6ed8
commit ecb26299f0
6 changed files with 120 additions and 0 deletions

24
src/VariableBindings.rs Normal file
View File

@@ -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
}

View File

@@ -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);
}

View File

@@ -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;
}

View File

@@ -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
}

View File

@@ -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);
}

View File

@@ -4,6 +4,7 @@
mod HelloWorld; mod HelloWorld;
mod Primitives; mod Primitives;
mod CustomTypes; mod CustomTypes;
mod VariableBindings;
fn main(){ fn main(){
@@ -42,4 +43,15 @@ fn main(){
//CustomTypes::Enums::TestCaseLinkedList::main(); //CustomTypes::Enums::TestCaseLinkedList::main();
//3.3 Constants //3.3 Constants
//CustomTypes::Constants::main(); //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();
} }