summaryrefslogtreecommitdiffstats
path: root/src/doc/book/listings/ch16-fearless-concurrency/listing-16-13
diff options
context:
space:
mode:
Diffstat (limited to 'src/doc/book/listings/ch16-fearless-concurrency/listing-16-13')
-rw-r--r--src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/Cargo.lock6
-rw-r--r--src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/Cargo.toml6
-rw-r--r--src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/output.txt15
-rw-r--r--src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/src/main.rs22
4 files changed, 49 insertions, 0 deletions
diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/Cargo.lock b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/Cargo.lock
new file mode 100644
index 000000000..8e7ba9cf5
--- /dev/null
+++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/Cargo.lock
@@ -0,0 +1,6 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+[[package]]
+name = "shared-state"
+version = "0.1.0"
+
diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/Cargo.toml b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/Cargo.toml
new file mode 100644
index 000000000..da297eaba
--- /dev/null
+++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "shared-state"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/output.txt b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/output.txt
new file mode 100644
index 000000000..ea6963903
--- /dev/null
+++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/output.txt
@@ -0,0 +1,15 @@
+$ cargo run
+ Compiling shared-state v0.1.0 (file:///projects/shared-state)
+error[E0382]: use of moved value: `counter`
+ --> src/main.rs:9:36
+ |
+5 | let counter = Mutex::new(0);
+ | ------- move occurs because `counter` has type `Mutex<i32>`, which does not implement the `Copy` trait
+...
+9 | let handle = thread::spawn(move || {
+ | ^^^^^^^ value moved into closure here, in previous iteration of loop
+10 | let mut num = counter.lock().unwrap();
+ | ------- use occurs due to use in closure
+
+For more information about this error, try `rustc --explain E0382`.
+error: could not compile `shared-state` due to previous error
diff --git a/src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/src/main.rs b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/src/main.rs
new file mode 100644
index 000000000..4e380a59a
--- /dev/null
+++ b/src/doc/book/listings/ch16-fearless-concurrency/listing-16-13/src/main.rs
@@ -0,0 +1,22 @@
+use std::sync::Mutex;
+use std::thread;
+
+fn main() {
+ let counter = Mutex::new(0);
+ let mut handles = vec![];
+
+ for _ in 0..10 {
+ let handle = thread::spawn(move || {
+ let mut num = counter.lock().unwrap();
+
+ *num += 1;
+ });
+ handles.push(handle);
+ }
+
+ for handle in handles {
+ handle.join().unwrap();
+ }
+
+ println!("Result: {}", *counter.lock().unwrap());
+}