diff options
Diffstat (limited to '')
| -rw-r--r-- | src/closures/Cargo.toml | 6 | ||||
| -rw-r--r-- | src/closures/src/main.rs | 34 |
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:#?}"); +} |
