1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-06-15 22:00:26 +02:00

Use explicit Arc::clone instead of calling .clone() method (#596)

This is to highlight that the clones are cheap: they run custom logic
in the reference counted type, they don’t do a deep clone.
This commit is contained in:
Martin Geisler
2023-04-27 14:46:09 -07:00
committed by GitHub
parent 6ade739651
commit b051b04afa
3 changed files with 8 additions and 8 deletions

View File

@ -1,6 +1,6 @@
# `Arc`
[`Arc<T>`][1] allows shared read-only access via its `clone` method:
[`Arc<T>`][1] allows shared read-only access via `Arc::clone`:
```rust,editable
use std::thread;
@ -10,7 +10,7 @@ fn main() {
let v = Arc::new(vec![10, 20, 30]);
let mut handles = Vec::new();
for _ in 1..5 {
let v = v.clone();
let v = Arc::clone(&v);
handles.push(thread::spawn(move || {
let thread_id = thread::current().id();
println!("{thread_id:?}: {v:?}");

View File

@ -7,7 +7,7 @@ use std::thread;
// use std::sync::{Arc, Mutex};
fn main() {
let mut v = vec![10, 20, 30];
let v = vec![10, 20, 30];
let handle = thread::spawn(|| {
v.push(10);
});
@ -29,7 +29,7 @@ use std::thread;
fn main() {
let v = Arc::new(Mutex::new(vec![10, 20, 30]));
let v2 = v.clone();
let v2 = Arc::clone(&v);
let handle = thread::spawn(move || {
let mut v2 = v2.lock().unwrap();
v2.push(10);