Created
July 28, 2021 18:58
-
-
Save spareslant/66d4b8f67c6e469194e80015b9451bff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let mut s1 = String::from("Hello"); | |
let len = calculate_length(&s1); | |
println!("The length of '{}' is {}.", s1, len); | |
change(&mut s1); | |
println!("The length of '{}' is {}.", s1, len); | |
references_example_1(); | |
references_example_1_1(); | |
// references_example_2(); | |
references_example_3(); | |
references_example_3_1(); | |
// references_example_3_2(); | |
// references_example_4(); | |
references_example_4_1(); | |
references_example_4_2(); | |
references_example_4_3(); | |
references_example_4_4(); | |
references_example_5(); | |
references_example_6(); | |
references_example_7(); | |
} | |
fn calculate_length(s: &String) -> usize { | |
s.len() | |
} | |
fn change(some_string: &mut String) { | |
some_string.push_str(" , World"); | |
} | |
// ib => b has borrowed immutably | |
// mb => b has borrowed mutably | |
// in all the following functions, b and c are mutable or immutable borrows of a | |
// we are only looking at the scopes of borrowed references. Not normal variable | |
fn references_example_1() { | |
println!("======== references_example_1 ========"); | |
let a = "yahoo"; | |
// b itself is mutable i.e we can assign a new value to it. | |
// but b is borrowing immutable a | |
let mut b = &a; // |ib | |
// println has created immutable // |ib | |
// reference to a and using it. // |ib | |
// b is already immutable ref to // |ib | |
// a // |ib | |
println!("a = {}, b = {}", a, b); // |ia |ib | |
// a = "hotmail"; // |ia |ib | |
// |ia |ib | |
b = &"oracle"; // |ia |ib | |
println!("a = {}, b = {}", a, b); // |ia |ib | |
// Note: only immutable references are being used so no compilation error if though | |
// scopes are overlapping. println creats one more immutable refernce of a and uses it. | |
} | |
fn references_example_1_1() { | |
println!("======== references_example_1_1 ========"); | |
let a = "yahoo"; | |
// b itself is mutable i.e we can assign a new value to it. | |
// but b is borrowing immutable a | |
let mut b = &a; // |ib | |
// |ib | |
// a = "hotmail"; // |ib | |
// |ib | |
b = &"oracle"; // |ib | |
println!("a = {}, b = {}", a, b); // |ia |ib | |
// Note: only immutable references are being used so no compilation error if though | |
// scopes are overlapping. println creats one more immutable refernce of a and uses it. | |
} | |
// fn references_example_1_2() { | |
// println!("======== references_example_1_2 ========"); | |
// let mut a = "yahoo"; | |
// // b itself is mutable i.e we can assign a new value to it. | |
// // b is also borrowing mutable a | |
// let mut b = &mut a; // |mb | |
// // |mb | |
// // a = "hotmail"; // |mb | |
// // |mb | |
// println!("a = {}, b = {}", a, b); // |ia |mb | |
// // Note: println uses immutable borrow of a. Also we are passing mutable borrow of a via b. | |
// // scopes are overlapping for mutable and immutable borrows. Compilation will fail | |
// // println creats one more immutable refernce of a and uses it. | |
// } | |
// fn references_example_2() { | |
// println!("======== references_example_2 ========"); | |
// let mut a = "yahoo"; | |
// // b itself is not mutable i.e we cannot assign a new value to it. | |
// // b is also borrowing mutable a | |
// let b = &mut a; // |mb | |
// // |mb | |
// println!("a = {}, b = {}", a, b); // |ia |mb | |
// // |mb | |
// println!("b = {}", b); // |mb | |
// *b = "oracle"; // |mb | |
// println!("b = {}", b); // |mb | |
// // |mb | |
// let c = &a; // |mb |ic | |
// // |mb |ic | |
// println!("a = {}", a); // |ia |mb |ic | |
// // |mb |ic | |
// println!("b = {}", b); // |mb |ic | |
// // |ic | |
// // |ic | |
// println!("c = {}", c); // |ic | |
// | |
// // Note: As mutable and immutable borrows of a (i.e. b and c respectively) scopes | |
// // overlaps. compailation will fail | |
// // println creats one more immutable refernce of a and uses it. | |
// } | |
fn references_example_3() { | |
println!("======== references_example_3 ========"); | |
let mut a = "yahoo"; | |
let b = &mut a; // |mb | |
// |mb | |
println!("b = {}", b); // |mb | |
println!("a = {}", a); // |ia | |
// Note: mutable and immutable borrows of a (i.e. b and c respectively) scopes | |
// are NOT overlapping. It will be compiled successfully. | |
// println creats one more immutable refernce of a and uses it. | |
} | |
fn references_example_3_1() { | |
println!("======== references_example_3_1 ========"); | |
let mut a = "yahoo"; | |
let c = &a; // |ic | |
println!("c = {}", c); // |ic | |
let b = &mut a; // |mb | |
println!("b = {}", b); // |mb | |
// | |
println!("a = {}", a); // |ia | |
// Note: mutable and immutable borrows of a (i.e. b and c respectively) scopes | |
// are NOT overlapping. It will be compiled successfully. | |
// println creats one more immutable refernce of a and uses it. | |
} | |
// fn references_example_3_2() { | |
// println!("======== references_example_3_2 ========"); | |
// let mut a = "yahoo"; | |
// let b = &mut a; // |mb | |
// // |mb | |
// // |mb | |
// println!("a = {}, b => {}", a, b);// |ia |mb | |
// | |
// // a `new immutable reference` of a is taken here. | |
// // Its scope starts here. b (mutable reference of a) is also being used here. | |
// // scope of b is still active. | |
// // scope of `new immutable refernece of a` and scope of b (mutable reference of a) ends here | |
// // but overlaps on each other also. | |
// // This will produce compile error. | |
// | |
// } | |
// fn references_example_4() { | |
// println!("======== references_example_4 ========"); | |
// let a = String::from("yahoo"); | |
// let mut b = &a; // |ib | |
// b = &String::from("oracle"); // |ib | |
// println!("a = {}, b = {}", a, b); // |ia |ib | |
// println!("b = {}", b); // |ia |ib | |
// println!("a = {}", a); // |ia | |
// | |
// // Note: Scopes are overlapping but both are immutable references. Hence compilation should | |
// // not fail due to this. | |
// // Howver compilation fails for other reason. (b = &String::from("oracle") causes compilation | |
// // to fail.) | |
// | |
// } | |
fn references_example_4_1() { | |
println!("======== references_example_4_1 ========"); | |
let a = String::from("yahoo"); | |
let mut b = &a; // |ib | |
let c = String::from("oracle"); // |ib | |
b = &c; // |ib | |
println!("a = {}, b = {}", a, b); // |ia |ib | |
// Note: Scopes are overlapping but both are immutable references. Hence compilation should | |
// not fail due to this. | |
// Also note that, b was first following from a and then from c. | |
// This is possible because we are using let mut b | |
} | |
fn references_example_4_2() { | |
println!("======== references_example_4_2 ========"); | |
let a = String::from("yahoo"); | |
// uncommenting following lines will produce compile error. Because String do not have Copy | |
// trait. | |
// let &b = &a; | |
let c = a; | |
// uncommenting following lines will produce compile error. Because c has taken the ownership | |
// from a. a is not valid anymore from this point onwards. | |
// println!("a = {}, c = {}", a, c); | |
println!("c = {}", c); // |ic | |
} | |
fn references_example_4_3() { | |
println!("======== references_example_4_3 ========"); | |
let a = "yahoo"; | |
// a type is &str | |
// b = &a => b type is &&str. therefore *b type becomes &str. | |
// &b = &a => b type is &str not &&str here. therefore *b type becomes str. | |
// &(mut b) = &a => b type is &str not &&str here. therefore *b type becomes str. | |
// b is NOT a immutable reference of a here. value of a is being copied in b | |
let &(mut b) = &a; // | |
println!("a = {}, b = {}", a, b); // |ia |ib | |
b = "oracle"; // | |
println!("a = {}, b = {}", a, b); // |ia |ib | |
// Note: Scopes are overlapping but both are immutable references. Hence compilation should | |
// not fail due to this. | |
} | |
fn references_example_4_4() { | |
println!("======== references_example_4_4 ========"); | |
let mut a = "yahoo"; | |
// a type is &str | |
// b = &a => b type is &&str. therefore *b type becomes &str. | |
// &b = &a => b type is &str not &&str here. therefore *b type becomes str. | |
// &mut (mut b) = &a => b type is &str not &&str here. therefore *b type becomes str. | |
// b is NOT a immutable reference of a here. value of a is being copied in b | |
let &mut (mut b) = &mut a; // | |
println!("a = {}, b = {}", a, b); // |ia |ib | |
// uncommenting following lines // | |
// will produce compile error. // | |
// *b = "oracle"; // | |
b = "oracle"; // | |
println!("a = {}, b = {}", a, b); // |ia |ib | |
} | |
fn references_example_5() { | |
println!("======== references_example_5 ========"); | |
let mut a = String::from("yahoo"); | |
let b = &mut a; // |mb | |
*b = String::from("oracle"); // |mb | |
//println!("a = {}, b = {}", a, b); // |mb | |
println!("b = {:p}, b = {}, *b = {}", b, b, *b); // |mb | |
println!("a = {}", a); // |ia | |
// Note: mutable and immutable borrows scopes | |
// are NOT overlapping. It will be compiled successfully. | |
// println creats one more immutable refernce of a and uses it. | |
} | |
fn references_example_6() { | |
println!("======== references_example_6 ========"); | |
let mut a = 5; | |
let b = &mut a; // |mb | |
*b = 7; // |mb | |
println!("b = {:p}, b = {}, *b = {}", b, b, *b); // |mb | |
println!("a = {}", a); // |ia | |
// Note: mutable and immutable borrows scopes | |
// are NOT overlapping. It will be compiled successfully. | |
// println creats one more immutable refernce of a and uses it. | |
} | |
fn references_example_7() { | |
println!("======== references_example_7 ========"); | |
let mut a = "yahoo"; | |
let mut b = &mut a; // |mb | |
let mut c = "oracle"; // |mb | |
// uncommenting following line // |mb | |
// will give compile error // |mb | |
// b = &mut "oracle"; // |mb | |
b = &mut c; // |mb | |
println!("a = {}", a); // |ia |mb | |
println!("b = {}", b); // |mb | |
println!("c = {}", c); // |ic | |
// Note: scope of mutable borrow of a (i.e b) overlaps with immutable a (created by println). But | |
// still it will NOT give compilation error, because b = &mut c has caused the b to borrow | |
// from c ("oracle") not from a ("yahoo") anymore. b is not longer borrowing from a. | |
} | |
// ********** OUTPUT ********** | |
// | |
// The length of 'Hello' is 5. | |
// The length of 'Hello , World' is 5. | |
// ======== references_example_1 ======== | |
// a = yahoo, b = yahoo | |
// a = yahoo, b = oracle | |
// ======== references_example_1_1 ======== | |
// a = yahoo, b = oracle | |
// ======== references_example_3 ======== | |
// b = yahoo | |
// a = yahoo | |
// ======== references_example_3_1 ======== | |
// c = yahoo | |
// b = yahoo | |
// a = yahoo | |
// ======== references_example_4_1 ======== | |
// a = yahoo, b = oracle | |
// ======== references_example_4_2 ======== | |
// c = yahoo | |
// ======== references_example_4_3 ======== | |
// a = yahoo, b = yahoo | |
// a = yahoo, b = oracle | |
// ======== references_example_4_4 ======== | |
// a = yahoo, b = yahoo | |
// a = yahoo, b = oracle | |
// ======== references_example_5 ======== | |
// b = 0x7ffee3d1ed78, b = oracle, *b = oracle | |
// a = oracle | |
// ======== references_example_6 ======== | |
// b = 0x7ffee3d1ede4, b = 7, *b = 7 | |
// a = 7 | |
// ======== references_example_7 ======== | |
// a = yahoo | |
// b = oracle | |
// c = oracle |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment