Skip to content

Commit 1391322

Browse files
author
Justo Hernández Romera
committed
Include Arc chapter.
1 parent 2e92719 commit 1391322

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@
182182
- [Alternate/custom key types](std/hash/alt_key_types.md)
183183
- [HashSet](std/hash/hashset.md)
184184
- [`Rc`](std/rc.md)
185+
- [`Arc`](std/arc.md)
185186

186187
- [Std misc](std_misc.md)
187188
- [Threads](std_misc/threads.md)

src/std/arc.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Arc
2+
3+
When shared ownership between threads is needed, `Arc`(Atomic Reference Counted) can be used. This struct, via the `Clone` implementation can create a reference pointer for the location of a value in the memory heap while increasing the reference counter. As it shares ownership between threads, when the last reference pointer to a value is out of scope, the variable is dropped.
4+
5+
```rust,editable
6+
7+
fn main() {
8+
use std::sync::Arc;
9+
use std::thread;
10+
11+
// This variable declaration is where it's value is specified.
12+
let apple = Arc::new("the same apple");
13+
14+
for _ in 0..10 {
15+
// Here there is no value specification as it is a pointer to a reference
16+
// in the memory heap.
17+
let apple = Arc::clone(&apple);
18+
19+
thread::spawn(move || {
20+
// As Arc was used, threads can be spawned using the value allocated
21+
// in the Arc variable pointer's location.
22+
println!("{:?}", apple);
23+
});
24+
}
25+
}
26+
27+
```

src/std/rc.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ fn main() {
4848

4949
### See also:
5050

51-
[std::rc][1] and [Arc][2].
51+
[std::rc][1] and [std::sync::arc][2].
5252

5353
[1]: https://doc.rust-lang.org/std/rc/index.html
54+
5455
[2]: https://doc.rust-lang.org/std/sync/struct.Arc.html

0 commit comments

Comments
 (0)