From 31be64135f56886df379b11d7373790d3dd72e57 Mon Sep 17 00:00:00 2001 From: hongyang <1664698982@qq.com> Date: Tue, 16 Apr 2024 22:27:52 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E7=AC=AC=E5=9B=9B=E8=AF=BE=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/task2/main.rs => main.rs | 0 src/task1/method1.rs | 60 ++++++++++++------------- src/task1/method2.rs | 86 ++++++++++++++++++------------------ 3 files changed, 73 insertions(+), 73 deletions(-) rename src/task2/main.rs => main.rs (100%) diff --git a/src/task2/main.rs b/main.rs similarity index 100% rename from src/task2/main.rs rename to main.rs diff --git a/src/task1/method1.rs b/src/task1/method1.rs index 76c4c12..88e33af 100644 --- a/src/task1/method1.rs +++ b/src/task1/method1.rs @@ -1,30 +1,30 @@ -// #[derive(Debug)] -// enum MyNumber { -// Int(i32), -// Float(f64), -// String(String), -// } -// -// impl MyNumber { -// fn add(&self, other: &Self) -> Self { -// match (self, other) { -// (MyNumber::Int(a), MyNumber::Int(b)) => MyNumber::Int(a + b), -// (MyNumber::Float(a), MyNumber::Float(b)) => MyNumber::Float(a + b), -// (MyNumber::String(a), MyNumber::String(b)) => MyNumber::String(format!("{}{}", a, b)), -// _ => unimplemented!("Cannot add these types"), -// } -// } -// } -// -// fn main() { -// let numbers = vec![ -// MyNumber::Int(5), -// MyNumber::Float(3.14), -// MyNumber::String(String::from("Hello, ")), -// ]; -// -// for number in numbers { -// let result = number.add(&MyNumber::Int(3)); -// println!("Result: {:?}", result); -// } -// } \ No newline at end of file +#[derive(Debug)] +enum MyNumber { + Int(i32), + Float(f64), + String(String), +} + +impl MyNumber { + fn add(&self, other: &Self) -> Self { + match (self, other) { + (MyNumber::Int(a), MyNumber::Int(b)) => MyNumber::Int(a + b), + (MyNumber::Float(a), MyNumber::Float(b)) => MyNumber::Float(a + b), + (MyNumber::String(a), MyNumber::String(b)) => MyNumber::String(format!("{}{}", a, b)), + _ => unimplemented!("Cannot add these types"), + } + } +} + +fn main() { + let numbers = vec![ + MyNumber::Int(5), + MyNumber::Float(3.14), + MyNumber::String(String::from("Hello, ")), + ]; + + for number in numbers { + let result = number.add(&MyNumber::Int(3)); + println!("Result: {:?}", result); + } +} \ No newline at end of file diff --git a/src/task1/method2.rs b/src/task1/method2.rs index 7a648a7..348d431 100644 --- a/src/task1/method2.rs +++ b/src/task1/method2.rs @@ -1,43 +1,43 @@ -// #[derive(Debug, Clone, Copy, PartialEq, Eq)] -// struct Point { -// x: i32, -// y: i32, -// } -// -// impl std::ops::Add for Point { -// type Output = Self; -// -// fn add(self, other: Self) -> Self { -// Point { -// x: self.x + other.x, -// y: self.y + other.y, -// } -// } -// } -// -// trait Addable { -// fn add(&self, other: &Self) -> Self; -// } -// -// impl Addable for Point { -// fn add(&self, other: &Self) -> Self { -// Point { -// x: self.x + other.x, -// y: self.y + other.y, -// } -// } -// } -// -// fn main() { -// let p1 = Point { x: 1, y: 2 }; -// let p2 = Point { x: 3, y: 4 }; -// -// // 使用 + 运算符直接相加 -// let p3 = p1 + p2; -// println!("p1 + p2 = {:?}", p3); -// -// // 使用 Trait Object 调用 add 方法 -// let addable: &dyn Addable = &p1; -// let p4 = addable.add(&p2); -// println!("Using Trait Object: {:?}", p4); -// } \ No newline at end of file +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +struct Point { + x: i32, + y: i32, +} + +impl std::ops::Add for Point { + type Output = Self; + + fn add(self, other: Self) -> Self { + Point { + x: self.x + other.x, + y: self.y + other.y, + } + } +} + +trait Addable { + fn add(&self, other: &Self) -> Self; +} + +impl Addable for Point { + fn add(&self, other: &Self) -> Self { + Point { + x: self.x + other.x, + y: self.y + other.y, + } + } +} + +fn main() { + let p1 = Point { x: 1, y: 2 }; + let p2 = Point { x: 3, y: 4 }; + + // 使用 + 运算符直接相加 + let p3 = p1 + p2; + println!("p1 + p2 = {:?}", p3); + + // 使用 Trait Object 调用 add 方法 + let addable: &dyn Addable = &p1; + let p4 = addable.add(&p2); + println!("Using Trait Object: {:?}", p4); +} \ No newline at end of file From 926d9dd3992e4cb244f49359b037b18e5a13b662 Mon Sep 17 00:00:00 2001 From: hongyang <1664698982@qq.com> Date: Tue, 16 Apr 2024 22:29:37 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E7=AC=AC=E5=9B=9B=E8=AF=BE=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.rs => src/task2/main.rs | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename main.rs => src/task2/main.rs (100%) diff --git a/main.rs b/src/task2/main.rs similarity index 100% rename from main.rs rename to src/task2/main.rs From 2c44e0efe1f4c7849fc7d0824347795314ea87f4 Mon Sep 17 00:00:00 2001 From: hongyang <1664698982@qq.com> Date: Tue, 16 Apr 2024 22:51:31 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E7=AC=AC=E4=BA=94=E8=AF=BE=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 11 +++++++++++ src/task1/method1.rs | 30 ------------------------------ src/task1/method2.rs | 43 ------------------------------------------- src/task2/main.rs | 43 ------------------------------------------- 4 files changed, 11 insertions(+), 116 deletions(-) create mode 100644 src/main.rs delete mode 100644 src/task1/method1.rs delete mode 100644 src/task1/method2.rs delete mode 100644 src/task2/main.rs diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..cf81ef3 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,11 @@ +#[macro_export] +macro_rules! log { + ($level:expr, $($arg:tt)*) => { + println!("[{}] {}", $level, format_args!($($arg)*)) + }; +} + +fn main() { + log!("debug", "This is a debug message"); + log!("error", "Something went wrong: {}", 404); +} \ No newline at end of file diff --git a/src/task1/method1.rs b/src/task1/method1.rs deleted file mode 100644 index 88e33af..0000000 --- a/src/task1/method1.rs +++ /dev/null @@ -1,30 +0,0 @@ -#[derive(Debug)] -enum MyNumber { - Int(i32), - Float(f64), - String(String), -} - -impl MyNumber { - fn add(&self, other: &Self) -> Self { - match (self, other) { - (MyNumber::Int(a), MyNumber::Int(b)) => MyNumber::Int(a + b), - (MyNumber::Float(a), MyNumber::Float(b)) => MyNumber::Float(a + b), - (MyNumber::String(a), MyNumber::String(b)) => MyNumber::String(format!("{}{}", a, b)), - _ => unimplemented!("Cannot add these types"), - } - } -} - -fn main() { - let numbers = vec![ - MyNumber::Int(5), - MyNumber::Float(3.14), - MyNumber::String(String::from("Hello, ")), - ]; - - for number in numbers { - let result = number.add(&MyNumber::Int(3)); - println!("Result: {:?}", result); - } -} \ No newline at end of file diff --git a/src/task1/method2.rs b/src/task1/method2.rs deleted file mode 100644 index 348d431..0000000 --- a/src/task1/method2.rs +++ /dev/null @@ -1,43 +0,0 @@ -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -struct Point { - x: i32, - y: i32, -} - -impl std::ops::Add for Point { - type Output = Self; - - fn add(self, other: Self) -> Self { - Point { - x: self.x + other.x, - y: self.y + other.y, - } - } -} - -trait Addable { - fn add(&self, other: &Self) -> Self; -} - -impl Addable for Point { - fn add(&self, other: &Self) -> Self { - Point { - x: self.x + other.x, - y: self.y + other.y, - } - } -} - -fn main() { - let p1 = Point { x: 1, y: 2 }; - let p2 = Point { x: 3, y: 4 }; - - // 使用 + 运算符直接相加 - let p3 = p1 + p2; - println!("p1 + p2 = {:?}", p3); - - // 使用 Trait Object 调用 add 方法 - let addable: &dyn Addable = &p1; - let p4 = addable.add(&p2); - println!("Using Trait Object: {:?}", p4); -} \ No newline at end of file diff --git a/src/task2/main.rs b/src/task2/main.rs deleted file mode 100644 index 348d431..0000000 --- a/src/task2/main.rs +++ /dev/null @@ -1,43 +0,0 @@ -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -struct Point { - x: i32, - y: i32, -} - -impl std::ops::Add for Point { - type Output = Self; - - fn add(self, other: Self) -> Self { - Point { - x: self.x + other.x, - y: self.y + other.y, - } - } -} - -trait Addable { - fn add(&self, other: &Self) -> Self; -} - -impl Addable for Point { - fn add(&self, other: &Self) -> Self { - Point { - x: self.x + other.x, - y: self.y + other.y, - } - } -} - -fn main() { - let p1 = Point { x: 1, y: 2 }; - let p2 = Point { x: 3, y: 4 }; - - // 使用 + 运算符直接相加 - let p3 = p1 + p2; - println!("p1 + p2 = {:?}", p3); - - // 使用 Trait Object 调用 add 方法 - let addable: &dyn Addable = &p1; - let p4 = addable.add(&p2); - println!("Using Trait Object: {:?}", p4); -} \ No newline at end of file