diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 7e00336d6b..093d3af8ee 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -182,6 +182,7 @@ - [Alternate/custom key types](std/hash/alt_key_types.md) - [HashSet](std/hash/hashset.md) - [`Rc`](std/rc.md) + - [`Arc`](std/arc.md) - [Std misc](std_misc.md) - [Threads](std_misc/threads.md) diff --git a/src/std/arc.md b/src/std/arc.md new file mode 100644 index 0000000000..566bd55eca --- /dev/null +++ b/src/std/arc.md @@ -0,0 +1,27 @@ +# Arc + +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. + +```rust,editable + +fn main() { +use std::sync::Arc; +use std::thread; + +// This variable declaration is where it's value is specified. +let apple = Arc::new("the same apple"); + +for _ in 0..10 { + // Here there is no value specification as it is a pointer to a reference + // in the memory heap. + let apple = Arc::clone(&apple); + + thread::spawn(move || { + // As Arc was used, threads can be spawned using the value allocated + // in the Arc variable pointer's location. + println!("{:?}", apple); + }); +} +} + +``` diff --git a/src/std/rc.md b/src/std/rc.md index 86e00e9081..a9691b2448 100644 --- a/src/std/rc.md +++ b/src/std/rc.md @@ -48,7 +48,7 @@ fn main() { ### See also: -[std::rc][1] and [Arc][2]. +[std::rc][1] and [std::sync::arc][2]. [1]: https://doc.rust-lang.org/std/rc/index.html [2]: https://doc.rust-lang.org/std/sync/struct.Arc.html