Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save spareslant/d2b411cb7de75e7c5fe76471881594f7 to your computer and use it in GitHub Desktop.
Save spareslant/d2b411cb7de75e7c5fe76471881594f7 to your computer and use it in GitHub Desktop.
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_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");
}
fn references_example_1() {
println!("======== references_example_1 ========");
let a = "yahoo";
let mut b = &a; // b is an mutable reference of a. Its scope starts here.
println!("a = {}, b = {}", a, b);
// a = "hotmail"; // variable a is immutable.
b = &"oracle"; // overwrite b with new reference (i.e. reference of 'static "oracle").
// Does not affect a
println!("a = {}, b = {}", a, b); // scope of b (mutable reference of a) ends here.
// Note: there is only one reference of a. So no question of refereneces scope overlapping.
}
fn references_example_2() {
println!("======== references_example_2 ========");
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.
let b = &mut a; // b is an mutable reference of a. Its scope starts here.
// uncommenting following code will produce compile error because
// println!("a = {}, b = {}", a, b); // a `new immutable reference` of a is taken here and is used.
// Its scope starts here. b (mutable reference of a) is also being used here.
// scope of `new immutable refernece of a` and scope of b (mutable reference of a) overlaps
// here. This will produce compile error.
println!("b = {}", b);
*b = "oracle";
println!("b = {}", b); // scope of b (mutable reference of a) ends here.
let c = &a; // c is an immutable reference of a. Its scope starts here.
println!("a = {}", a); // a is NOT a reference. Just a normal variable.
// If following line is UNCOMMETED.
// println!("b = {}", b); // b (mutable reference of a) used here. hence scope of b still active here. Scope of c (immutable refenrece of a) is also running. Scope of b and c overlaps, produces compile error.
println!("c = {}", c); // scope of c (immutable reference of a) ends here.
// Note: scope of b (mutable reference of a) and scope of c (immutable reference of a) do NOT
// overlap.
}
fn references_example_3() {
println!("======== references_example_3 ========");
let mut a = "yahoo";
let b = &mut a; // b is an mutable reference of a. Its scope starts here.
//println!("a = {}, b => {}", a, b); // a `new immutable reference` of a is taken here and is used.
// Its scope starts here. b (mutable reference of a) is also being used here.
// scope of `new immutable refernece of a` and scope of b (mutable reference of a) overlaps
// here. This will produce compile error.
println!("b = {}", b); // scope of b(mutable reference of a) ends here.
println!("a = {}", a); // a is NOT a reference. Just a normal variable.
// So scope overlapping does not apply here.
// Note: scope overlapping comes into picture only for references.
}
fn references_example_3_1() {
println!("======== references_example_3_1 ========");
let mut a = "yahoo";
let c = &a; // c is an immutable reference of a. Its scope starts here.
println!("c = {}", c); // scope of c ends here. Not used any more in this code block.
let b = &mut a; // b is an mutable reference of a. Its scope starts here.
println!("b = {}", b); // scope of a ends here. Not used any more in this code block.
// Note: Above, b and c borrow from same data (i.e a) as immutable and mutable
// references. But their scopes do not overlap on each other. Hence this is valid.
println!("a = {}", a); // a is NOT reference. Just a normal variable.
}
fn references_example_3_2() {
println!("======== references_example_3_2 ========");
let mut a = "yahoo";
let b = &mut a; // b is an mutable reference of a. Its scope starts here.
// println!("a = {}, b => {}", a, b); // 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;
b = &String::from("oracle");
// uncommenting following lines will produce compile error.
// println!("a = {}, b = {}", a, b);
// println!("b = {}", b);
println!("a = {}", a);
}
fn references_example_4_1() {
println!("======== references_example_4_1 ========");
let a = String::from("yahoo");
let mut b = &a;
let c = String::from("oracle");
b = &c;
println!("a = {}, b = {}", a, 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);
}
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.
let &(mut b) = &a;
println!("a = {}, b = {}", a, b);
b = "oracle";
println!("a = {}, b = {}", a, b);
}
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.
let &mut (mut b) = &mut a;
println!("a = {}, b = {}", a, b);
// uncommenting following lines will produce compile error.
// *b = "oracle";
b = "oracle";
println!("a = {}, b = {}", a, b);
}
fn references_example_5() {
println!("======== references_example_5 ========");
let mut a = String::from("yahoo");
let b = &mut a;
*b = String::from("oracle");
//println!("a = {}, b = {}", a, b);
println!("b = {:p}, b = {}, *b = {}", b, b, *b);
println!("a = {}", a);
}
fn references_example_6() {
println!("======== references_example_6 ========");
let mut a = 5;
let b = &mut a;
*b = 7;
println!("b = {:p}, b = {}, *b = {}", b, b, *b);
println!("a = {}", a);
}
fn references_example_7() {
println!("======== references_example_7 ========");
let mut a = "yahoo";
let mut b = &mut a;
let mut c = "oracle";
// uncommenting following line will give compile error
// b = &mut "oracle";
b = &mut c;
println!("a = {}", a);
println!("b = {}", b);
println!("c = {}", c);
}
// ************* 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_2 ========
// b = yahoo
// b = oracle
// a = oracle
// c = oracle
// ======== references_example_3 ========
// b = yahoo
// a = yahoo
// ======== references_example_3_1 ========
// c = yahoo
// b = yahoo
// a = yahoo
// ======== references_example_3_2 ========
// ======== references_example_4 ========
// 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 = 0x7ffee36e0da8, b = oracle, *b = oracle
// a = oracle
// ======== references_example_6 ========
// b = 0x7ffee36e0e14, 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