summaryrefslogtreecommitdiffstats
path: root/src/doc/book/listings/ch17-oop/listing-17-10
diff options
context:
space:
mode:
Diffstat (limited to 'src/doc/book/listings/ch17-oop/listing-17-10')
-rw-r--r--src/doc/book/listings/ch17-oop/listing-17-10/Cargo.lock6
-rw-r--r--src/doc/book/listings/ch17-oop/listing-17-10/Cargo.toml6
-rw-r--r--src/doc/book/listings/ch17-oop/listing-17-10/output.txt13
-rw-r--r--src/doc/book/listings/ch17-oop/listing-17-10/src/lib.rs27
-rw-r--r--src/doc/book/listings/ch17-oop/listing-17-10/src/main.rs9
5 files changed, 61 insertions, 0 deletions
diff --git a/src/doc/book/listings/ch17-oop/listing-17-10/Cargo.lock b/src/doc/book/listings/ch17-oop/listing-17-10/Cargo.lock
new file mode 100644
index 000000000..00d7b2182
--- /dev/null
+++ b/src/doc/book/listings/ch17-oop/listing-17-10/Cargo.lock
@@ -0,0 +1,6 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+[[package]]
+name = "gui"
+version = "0.1.0"
+
diff --git a/src/doc/book/listings/ch17-oop/listing-17-10/Cargo.toml b/src/doc/book/listings/ch17-oop/listing-17-10/Cargo.toml
new file mode 100644
index 000000000..9b816e766
--- /dev/null
+++ b/src/doc/book/listings/ch17-oop/listing-17-10/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "gui"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
diff --git a/src/doc/book/listings/ch17-oop/listing-17-10/output.txt b/src/doc/book/listings/ch17-oop/listing-17-10/output.txt
new file mode 100644
index 000000000..74330fa0a
--- /dev/null
+++ b/src/doc/book/listings/ch17-oop/listing-17-10/output.txt
@@ -0,0 +1,13 @@
+$ cargo run
+ Compiling gui v0.1.0 (file:///projects/gui)
+error[E0277]: the trait bound `String: Draw` is not satisfied
+ --> src/main.rs:5:26
+ |
+5 | components: vec![Box::new(String::from("Hi"))],
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Draw` is not implemented for `String`
+ |
+ = help: the trait `Draw` is implemented for `Button`
+ = note: required for the cast to the object type `dyn Draw`
+
+For more information about this error, try `rustc --explain E0277`.
+error: could not compile `gui` due to previous error
diff --git a/src/doc/book/listings/ch17-oop/listing-17-10/src/lib.rs b/src/doc/book/listings/ch17-oop/listing-17-10/src/lib.rs
new file mode 100644
index 000000000..960fee23d
--- /dev/null
+++ b/src/doc/book/listings/ch17-oop/listing-17-10/src/lib.rs
@@ -0,0 +1,27 @@
+pub trait Draw {
+ fn draw(&self);
+}
+
+pub struct Screen {
+ pub components: Vec<Box<dyn Draw>>,
+}
+
+impl Screen {
+ pub fn run(&self) {
+ for component in self.components.iter() {
+ component.draw();
+ }
+ }
+}
+
+pub struct Button {
+ pub width: u32,
+ pub height: u32,
+ pub label: String,
+}
+
+impl Draw for Button {
+ fn draw(&self) {
+ // code to actually draw a button
+ }
+}
diff --git a/src/doc/book/listings/ch17-oop/listing-17-10/src/main.rs b/src/doc/book/listings/ch17-oop/listing-17-10/src/main.rs
new file mode 100644
index 000000000..2ede87ab7
--- /dev/null
+++ b/src/doc/book/listings/ch17-oop/listing-17-10/src/main.rs
@@ -0,0 +1,9 @@
+use gui::Screen;
+
+fn main() {
+ let screen = Screen {
+ components: vec![Box::new(String::from("Hi"))],
+ };
+
+ screen.run();
+}