advancedSmart Pointers
Rc<T> Reference Counting
Share ownership with reference counting.
Rc<T> Reference Counting
Share ownership with reference counting.
Difficulty
Advanced
Code
rust
use std::rc::Rc;
fn main() {
let a = Rc::new(vec![1, 2, 3]);
let b = Rc::clone(&a);
let c = Rc::clone(&a);
println!("a: {:?}", a);
println!("b: {:?}", b);
println!("reference count: {}", Rc::strong_count(&a));
}Explanation
This example demonstrates how to use rc
Key Concepts
- Rust's strong type system catches errors at compile time
- Ownership and borrowing rules ensure memory safety
- Pattern matching makes code expressive and exhaustive
Related Topics
Browse more examples in the smart-pointers category to build a complete understanding of this topic.