misc/process/wait.html)
    9. [ **20.6.** Filesystem Operations ](../std_misc/fs.html)
    10. [ **20.7.** Program arguments ](../std_misc/arg.html) â± 
    11.       1. [ **20.7.1.** Argument parsing ](../std_misc/arg/matching.html)
    12. [ **20.8.** Foreign Function Interface ](../std_misc/ffi.html)
  41. [ **21.** Testing ](../testing.html) â± 
  42.     1. [ **21.1.** Unit testing ](../testing/unit_testing.html)
    2. [ **21.2.** Documentation testing ](../testing/doc_testing.html)
    3. [ **21.3.** Integration testing ](../testing/integration_testing.html)
    4. [ **21.4.** Dev-dependencies ](../testing/dev_dependencies.html)
  43. [ **22.** Unsafe Operations ](../unsafe.html) â± 
  44.     1. [ **22.1.** Inline assembly ](../unsafe/asm.html)
  45. [ **23.** Compatibility ](../compatibility.html) â± 
  46.     1. [ **23.1.** Raw identifiers ](../compatibility/raw_identifiers.html)
  47. [ **24.** Meta ](../meta.html) â± 
  48.     1. [ **24.1.** Documentation ](../meta/doc.html)
    2. [ **24.2.** Playground ](../meta/playground.html)

__ __

  * Light (default) 
  * Rust 
  * Coal 
  * Navy 
  * Ayu 

__

#  Rust By Example

[ __ ](../print.html "Print this book") [ __ ](https://github.com/rust-
lang/rust-by-example "Git repository") [ __ ](https://github.com/rust-
lang/rust-by-example/edit/master/src/std/rc.md "Suggest an edit")

#  ` Rc `

When multiple ownership is needed, ` Rc ` (Reference Counting) can be used. `
Rc ` keeps track of the number of the references which means the number of
owners of the value wrapped inside an ` Rc ` .

Reference count of an ` Rc ` increases by 1 whenever an ` Rc ` is cloned, and
decreases by 1 whenever one cloned ` Rc ` is dropped out of the scope. When an
` Rc ` 's reference count becomes zero (which means there are no remaining
owners), both the ` Rc ` and the value are all dropped.

Cloning an ` Rc ` never performs a deep copy. Cloning creates just another
pointer to the wrapped value, and increments the count.

    
    
    use std::rc::Rc;
    
    fn main() {
        let rc_examples = "Rc examples".to_string();
        {
            println!("--- rc_a is created ---");
            
            let rc_a: Rc<String> = Rc::new(rc_examples);
            println!("Reference Count of rc_a: {}", Rc::strong_count(&rc_a));
            
            {
                println!("--- rc_a is cloned to rc_b ---");
                
                let rc_b: Rc<String> = Rc::clone(&rc_a);
                println!("Reference Count of rc_b: {}", Rc::strong_count(&rc_b));
                println!("Reference Count of rc_a: {}", Rc::strong_count(&rc_a));
                
                // Two `Rc`s are equal if their inner values are equal
                println!("rc_a and rc_b are equal: {}", rc_a.eq(&rc_b));
                
                // We can use methods of a value directly
                println!("Length of the value inside rc_a: {}", rc_a.len());
                println!("Value of rc_b: {}", rc_b);
                
                println!("--- rc_b is dropped out of scope ---");
            }
            
            println!("Reference Count of rc_a: {}", Rc::strong_count(&rc_a));
            
            println!("--- rc_a is dropped out of scope ---");
        }
        
        // Error! `rc_examples` already moved into `rc_a`
        // And when `rc_a` is dropped, `rc_examples` is dropped together
        // println!("rc_examples: {}", rc_examples);
        // TODO ^ Try uncommenting this line
    }
    

###  See also:

[ std::rc ](https://doc.rust-lang.org/std/rc/index.html) and [ std::sync::arc
](https://doc.rust-lang.org/std/sync/struct.Arc.html) .

[ __ ](../std/hash/hashset.html "Previous chapter") [ __ ](../std/arc.html
"Next chapter")

[ __ ](../std/hash/hashset.html "Previous chapter") [ __ ](../std/arc.html
"Next chapter")