std-method

Rust Rc::strong_count and clone

Inspect the number of owning Rc handles. Rc::clone increases that number rather than copying the payload. This is a useful diagnostic in a single-threaded ownership graph, not an instruction to manually adjust counts. Expected output (not executed): 1 2 1 Why: Each clone adds one owner and its destruction removes one. Common mistake: Counting a borrowed &Rc as an additional owner.

Formal Syntax & Structure

rust syntaxrust
Rc::strong_count(&handle) -> usize

Working Example

Example Snippetrust
use std::rc::Rc;fn main() {    let item = Rc::new(vec![2, 4]);    println!("{}", Rc::strong_count(&item));    let backup = Rc::clone(&item);    println!("{}", Rc::strong_count(&item));    drop(backup);    println!("{}", Rc::strong_count(&item));}
Rust Reference