Rc & Arc
Rc<T> reference-counts for single-threaded sharing. Arc<T> uses atomic refcount for thread-safe sharing.
Busca en todas las páginas de la documentación
Rc<T> reference-counts for single-threaded sharing. Arc<T> uses atomic refcount for thread-safe sharing.
use std::sync::Arc;
use std::thread;
fn main() {
let data = Arc::new(vec![1, 2, 3]);
let d = Arc::clone(&data);
thread::spawn(move || println!("{:?}", d));
println!("{:?}", data);
}When to reach for this: Multiple owners of immutable data; Arc across threads.
use std::rc::Rc;
struct Node { value: i32, next: Option<Rc<Node>> }
fn main() {
let tail = Rc::new(Node { value: 2, next: None });
let head = Rc::new(Node { value: 1, next: Some(Rc::clone(&tail)) });
println!("{}", head.value);
}What this demonstrates:
Rc::clone increments count, shallow cloneOption<Rc<Node>> for singly-linked structureRc not Send. Arc is Send + Sync if T: Send + Sync. get_mut unique when strong_count==1.
Arc.Mutex/RwLock inside. Fix: Arc<Mutex<T>>.Weak.Rc::clone not deep clone of inner.None.| Alternative | Use When | Don't Use When |
|---|---|---|
Borrow &T | Single owner lifetime | Multiple owners |
Arena + indices | Graphs without refcount | Simple share |
Cow | Mostly read | True shared ownership |
Stack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.
Revisado por Chris St. John·Última actualización: 19 jul 2026