diff --git a/main.rs b/main.rs index ba86bdf..94a40da 100644 --- a/main.rs +++ b/main.rs @@ -241,6 +241,7 @@ fn main() { // Booleans can have either the value true or false let _is_true = true; + // Prefixing with an underscore means the variable may no longer be used // Characters are defined with single quotes // They can store most any type of character from any language @@ -481,7 +482,7 @@ fn main() { } } - // Use enum to store todays day + // Use enum to store today's day let today:Day = Day::Monday; // Perform different actions based on day @@ -508,6 +509,9 @@ fn main() { // Create a vector with defined values let mut vec2 = vec![1, 2, 3, 4]; + // Create a vector with defined values (2) and specific length (20) + let _vec3 = vec![2; 20]; + // Add values to the end of a vector vec2.push(5); @@ -537,6 +541,9 @@ fn main() { // Remove and return the last value println!("Pop {:?}", vec2.pop()); + // Remove and return the ith value + println!("Remove {:?}", vec2.remove(1)); + // START HERE // ----- FUNCTIONS ----- @@ -598,7 +605,7 @@ fn main() { // The above doesn't apply with data types : // Integers, bool, char, floats, tuples with the above data types only - // Here the string was borrowed by the function + // Here the ownership of the string was taken by the function let str3: String = String::from("World"); print_str(str3); @@ -619,6 +626,23 @@ fn main() { let mut str6: String = String::from("Derek"); change_string(&mut str6); + let str7 = &mut str6; + println!("{}", str7); + + let str8 = &mut str6; + // This throws an error because we borrow str6 as mutable more than once at a time + // println!("{}, {}", str7, str8); + + println!("{}", str8); // no problem + + let str9 = &str6; + // This throws an error because we borrow str6 as immutable which is also borrowed as mutable + // println!("{}, {}", str8, str9); + + // RULES + // 1. A reference’s scope starts from where it is introduced to the last time use. + // 2. Anytime, you can have either one mutable reference or any number of immutable references. + // ----- HASH MAPS ----- // Hash maps are used to store key / value pairs use std::collections::HashMap; @@ -649,6 +673,28 @@ fn main() { } } + + // Change the value with key + if let Some(x) = heroes.get_mut(&"Superman") { + // We can use if let to match one pattern while ignoring the rest + + // To reach the place where the mutable reference x is, + // we use *x, which is called dereferencing + *x = "Kirk Alyn"; + } + + // The above is equivalent to the following code + match heroes.get_mut(&"Superman") { + Some(x) => *x = "Kirk Alyn", + _ => (), + } + + // Remove the value with key + match heroes.remove(&"Batman") { + Some(x) => println!("{}, Have a good day", x), + None => println!("Batman is not a hero"), + } + // ----- STRUCTS ----- // A struct is a custom data type that stores multiple // types of data