On this page
Arc
Prev: `Box` | Up: smart_pointers | Next: `Rc`
Rel: smart_pointers, rust/builtins
Ref: Rust std — Arc
Tags: #public #prog #rust #stdlib #smart_pointers #tier/core
Signature & Overview
Arc<T>
Atomically reference-counted pointer for thread-safe shared immutable ownership (Send + Sync).
Notes & Pitfalls
- Thread-safe atomic reference counting: Share immutable data across threads cheaply via
Arc::clone(&a)(clones the pointer, not the data). - For shared mutable state, pair with
MutexorRwLock:Arc<Mutex<T>>(watch for reference cycles—useWeak<T>to break them).
Example
use std::sync::Arc;
use std::thread;
fn main() {
let config = Arc::new(String::from("prod"));
let c = Arc::clone(&config);
let len = thread::spawn(move || c.len()).join().unwrap();
assert_eq!(len, 4);
assert_eq!(*config, "prod");
}
Linked from
Related notes
- tuts/rust/std/smart_pointers/Box Linked here; links to this page; shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/smart_pointers/Rc Linked here; links to this page; shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/smart_pointers Linked here; links to this page; shared tags: #prog, #rust, #stdlib
- tuts/rust/builtins Linked here; shared tags: #prog, #rust
- tuts/rust/tutorials/concurrent_worker_pool_channels links to this page; shared tags: #prog, #rust
- tuts/rust/std/collections/BinaryHeap shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/collections/BTreeMap shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/collections/HashMap shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/collections/HashSet shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/collections/VecDeque shared tags: #prog, #rust, #stdlib, #tier