summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorhybrid <hybrid@hybridlabs.cc>2026-10-16 12:37:15 +0300
committerhybrid <hybrid@hybridlabs.cc>2026-10-16 12:37:15 +0300
commit256ad69edea8bb5eeff38e3f5fa1dcf6e990daf7 (patch)
tree1341d67aa4b4fcdf3ec21306b90ee894b721bbaa /src
parent43ddbb814e679bf2adde3a8602624c207641dca2 (diff)
downloadrust-book-256ad69edea8bb5eeff38e3f5fa1dcf6e990daf7.tar.gz
rust-book-256ad69edea8bb5eeff38e3f5fa1dcf6e990daf7.tar.bz2
rust-book-256ad69edea8bb5eeff38e3f5fa1dcf6e990daf7.zip
add: generics, panic
Diffstat (limited to 'src')
-rw-r--r--src/generics/Cargo.toml6
-rw-r--r--src/generics/src/main.rs22
-rw-r--r--src/panic/Cargo.toml6
-rw-r--r--src/panic/src/main.rs23
4 files changed, 57 insertions, 0 deletions
diff --git a/src/generics/Cargo.toml b/src/generics/Cargo.toml
new file mode 100644
index 0000000..9e86772
--- /dev/null
+++ b/src/generics/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "generics"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
diff --git a/src/generics/src/main.rs b/src/generics/src/main.rs
new file mode 100644
index 0000000..86b0281
--- /dev/null
+++ b/src/generics/src/main.rs
@@ -0,0 +1,22 @@
+struct Point<X1, Y1> {
+ x: X1,
+ y: Y1,
+}
+
+impl<X1, Y1> Point<X1, Y1> {
+ fn mixup<X2, Y2>(self, other: Point<X2, Y2>) -> Point<X1, Y2> {
+ Point {
+ x: self.x,
+ y: other.y,
+ }
+ }
+}
+
+fn main() {
+ let p1 = Point { x: 5, y: 10.4 };
+ let p2 = Point { x: "Hello", y: 'c' };
+
+ let p3 = p1.mixup(p2);
+
+ println!("p3.x = {}, p3.y = {}", p3.x, p3.y);
+}
diff --git a/src/panic/Cargo.toml b/src/panic/Cargo.toml
new file mode 100644
index 0000000..94b8cff
--- /dev/null
+++ b/src/panic/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "panic"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
diff --git a/src/panic/src/main.rs b/src/panic/src/main.rs
new file mode 100644
index 0000000..bf8be55
--- /dev/null
+++ b/src/panic/src/main.rs
@@ -0,0 +1,23 @@
+struct Test <T> {
+ a: T,
+ b: T,
+}
+
+#![allow(unused)]
+fn main() {
+ use std::fs::File;
+ use std::io::{self, Read};
+
+ fn read_username_from_file() -> Result<String, io::Error> {
+ let username_file_result = File::open("/tmp/hello.txt");
+
+ let mut username_file = username_file_result?;
+
+ let mut username = String::new();
+
+ username_file.read_to_string(&mut username)?;
+ Ok(username)
+ }
+
+ println!("username: {}", read_username_from_file().unwrap());
+}