Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created October 27, 2019 13:42
Show Gist options
  • Save rust-play/d1a94cea570e927b9dabedfd2069470a to your computer and use it in GitHub Desktop.
Save rust-play/d1a94cea570e927b9dabedfd2069470a to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use std::rc::Rc;
#[derive(Debug)]
struct Foo {
bar: usize,
}
impl Foo {
fn rc(bar: usize) -> Rc<Self> {
Rc::new(Foo { bar })
}
}
fn main(){
let mut a = vec![Foo::rc(1), Foo::rc(2), Foo::rc(3)];
let b = vec![Foo::rc(4), Foo::rc(5)];
println!("Before:");
println!("{:?}", a);
println!("{:?}\n", b);
a.append(&mut b.clone());
println!("After:");
println!("{:?}", a);
println!("{:?}\n", b);
println!("Strong references to b[0] / a[3]: {}", Rc::strong_count(&b[0]))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment