summaryrefslogtreecommitdiffstats
path: root/src/doc/book/listings/ch13-functional-features/listing-13-08
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /src/doc/book/listings/ch13-functional-features/listing-13-08
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/doc/book/listings/ch13-functional-features/listing-13-08')
-rw-r--r--src/doc/book/listings/ch13-functional-features/listing-13-08/.rustfmt.toml2
-rw-r--r--src/doc/book/listings/ch13-functional-features/listing-13-08/Cargo.toml8
-rw-r--r--src/doc/book/listings/ch13-functional-features/listing-13-08/output.txt18
-rw-r--r--src/doc/book/listings/ch13-functional-features/listing-13-08/src/main.rs22
4 files changed, 50 insertions, 0 deletions
diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-08/.rustfmt.toml b/src/doc/book/listings/ch13-functional-features/listing-13-08/.rustfmt.toml
new file mode 100644
index 000000000..ee10c634b
--- /dev/null
+++ b/src/doc/book/listings/ch13-functional-features/listing-13-08/.rustfmt.toml
@@ -0,0 +1,2 @@
+struct_lit_width = 50
+
diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-08/Cargo.toml b/src/doc/book/listings/ch13-functional-features/listing-13-08/Cargo.toml
new file mode 100644
index 000000000..703c9d977
--- /dev/null
+++ b/src/doc/book/listings/ch13-functional-features/listing-13-08/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "rectangles"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-08/output.txt b/src/doc/book/listings/ch13-functional-features/listing-13-08/output.txt
new file mode 100644
index 000000000..7a39ac618
--- /dev/null
+++ b/src/doc/book/listings/ch13-functional-features/listing-13-08/output.txt
@@ -0,0 +1,18 @@
+$ cargo run
+ Compiling rectangles v0.1.0 (file:///projects/rectangles)
+error[E0507]: cannot move out of `value`, a captured variable in an `FnMut` closure
+ --> src/main.rs:18:30
+ |
+15 | let value = String::from("by key called");
+ | ----- captured outer variable
+16 |
+17 | list.sort_by_key(|r| {
+ | ______________________-
+18 | | sort_operations.push(value);
+ | | ^^^^^ move occurs because `value` has type `String`, which does not implement the `Copy` trait
+19 | | r.width
+20 | | });
+ | |_____- captured by this `FnMut` closure
+
+For more information about this error, try `rustc --explain E0507`.
+error: could not compile `rectangles` due to previous error
diff --git a/src/doc/book/listings/ch13-functional-features/listing-13-08/src/main.rs b/src/doc/book/listings/ch13-functional-features/listing-13-08/src/main.rs
new file mode 100644
index 000000000..3b9c9cbdf
--- /dev/null
+++ b/src/doc/book/listings/ch13-functional-features/listing-13-08/src/main.rs
@@ -0,0 +1,22 @@
+#[derive(Debug)]
+struct Rectangle {
+ width: u32,
+ height: u32,
+}
+
+fn main() {
+ let mut list = [
+ Rectangle { width: 10, height: 1 },
+ Rectangle { width: 3, height: 5 },
+ Rectangle { width: 7, height: 12 },
+ ];
+
+ let mut sort_operations = vec![];
+ let value = String::from("by key called");
+
+ list.sort_by_key(|r| {
+ sort_operations.push(value);
+ r.width
+ });
+ println!("{:#?}", list);
+}