Created
January 6, 2025 02:22
-
-
Save 0ex-d/46e1b674aba07e27b47171e09fa4d18a to your computer and use it in GitHub Desktop.
Copy & Clone : Various methods of keeping track of References (both single threaded(Rc) and multi-threaded (Arc,Mutex)
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 p1 = 1_000; | |
let p2 = p1; // Copy | |
let v1 = Box::new(200); stack pointer but value on the heap | |
let v2 = v1; // Clone | |
} |
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
use std::rc::Rc; | |
fn main(){ | |
let p1 = Rc::new(5); | |
let p2 = Rc::clone(&p1); // ref only 'Clone' | |
let v1 = Rc::new(5); | |
let v2 = v1.clone(); // both ref & value Clone | |
println!("p1: {:?}, p2: {:?}",p1,p2); | |
println!("v1: {:?}, v2: {:?}",v1,v2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment