Back to questions
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
use std::rc::Rc;
fn main() {
let a = Rc::new(1);
let _b = Rc::clone(&a);
{
let _c = Rc::clone(&a);
println!("{}", Rc::strong_count(&a));
}
println!("{}", Rc::strong_count(&a));
}3 2
- Expecting
3 3:_cbelongs to the inner block, and when the block ends_cis dropped, which lowers the strong count by one. - Expecting
Rc::cloneto copy the value: it adds another owner of the same value and increments the count, and the originalacounts too.
- What would the program print if
let _bwere written aslet _? - Why do
Rc::clone(&a)anda.clone()do the same thing, and why is the first preferred?