summaryrefslogtreecommitdiffstats
path: root/src/closures
diff options
context:
space:
mode:
authorhybrid <hybrid@hybridlabs.cc>2026-10-31 03:54:43 +0300
committerhybrid <hybrid@hybridlabs.cc>2026-10-31 03:54:43 +0300
commit62dcfb1d9f95503f8eb8be1b29fa1d75bfcc27e2 (patch)
treeb8461eda6cce3f9bbfb2261dd828077e17c7d226 /src/closures
parent18ad654357d6cca34782aaa269f5e8e06cbe9241 (diff)
downloadrust-book-62dcfb1d9f95503f8eb8be1b29fa1d75bfcc27e2.tar.gz
rust-book-62dcfb1d9f95503f8eb8be1b29fa1d75bfcc27e2.tar.bz2
rust-book-62dcfb1d9f95503f8eb8be1b29fa1d75bfcc27e2.zip
add: 7 craits
smartpointers interior_mutability threads mutex hello-async async_concurrency multi_futures
Diffstat (limited to 'src/closures')
-rw-r--r--src/closures/Cargo.toml6
-rw-r--r--src/closures/src/main.rs34
2 files changed, 40 insertions, 0 deletions
diff --git a/src/closures/Cargo.toml b/src/closures/Cargo.toml
new file mode 100644
index 0000000..a48c2e4
--- /dev/null
+++ b/src/closures/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "closures"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
diff --git a/src/closures/src/main.rs b/src/closures/src/main.rs
new file mode 100644
index 0000000..c16cf8b
--- /dev/null
+++ b/src/closures/src/main.rs
@@ -0,0 +1,34 @@
+use std::thread;
+
+#[derive(Debug)]
+struct Rectangle {
+ width: u32,
+ height: u32,
+}
+
+fn main() {
+ let mut list_1 = vec![1, 2, 3];
+ println!("[list_1] Before defining closure: {list_1:?}");
+
+ let mut borrows_mutably = || list_1.push(7);
+
+ borrows_mutably();
+
+ println!("[list_1] After calling closure: {list_1:?}");
+
+ let list_2 = vec![1, 2, 3];
+ println!("[list_2] Before defining closure: {list_2:?}");
+
+ thread::spawn(move || println!("[list_2] From thread: {list_2:?}"))
+ .join()
+ .unwrap();
+
+ let mut list_3 = [
+ Rectangle { width: 10, height: 1 },
+ Rectangle { width: 3, height: 5 },
+ Rectangle { width: 7, height: 12 },
+ ];
+
+ list_3.sort_by_key(|r| r.width);
+ println!("{list_3:#?}");
+}