From dfea5a376b23769e94800a612150d351ccf2a322 Mon Sep 17 00:00:00 2001 From: Brian Obot Date: Fri, 6 Feb 2026 19:14:47 +0100 Subject: [PATCH 1/2] Add Notes on Operator overloading --- src/bin/20b_operator_overloading.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/bin/20b_operator_overloading.rs b/src/bin/20b_operator_overloading.rs index d24e855..46b51e7 100644 --- a/src/bin/20b_operator_overloading.rs +++ b/src/bin/20b_operator_overloading.rs @@ -86,6 +86,11 @@ fn main() -> Result<(), Box> { let _b = a[0]; println!("A = {a:?}"); + // not all operators can be overloaded in python + // & is always a reference + // && || are limited to boolean values + // .., ..= always create a range struct + // = is the assignment operator, which copies or moves a value Ok(()) } From 9da6c5373d1b8014ac2b246bc111818e1fa456e8 Mon Sep 17 00:00:00 2001 From: Brian Obot Date: Fri, 6 Feb 2026 19:54:07 +0100 Subject: [PATCH 2/2] Note on Drop trait --- src/bin/41_utility_traits.rs | 39 ++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/bin/41_utility_traits.rs diff --git a/src/bin/41_utility_traits.rs b/src/bin/41_utility_traits.rs new file mode 100644 index 0000000..2464aa1 --- /dev/null +++ b/src/bin/41_utility_traits.rs @@ -0,0 +1,39 @@ +fn main() { + // Traits, Description + // Drop: Clean up code rust runs automatically when a value is dropped + // Size: marker trait for types with a known size at compile time as opposed to dynamically sized types (slices) + // Clone: type that support cloning + // Copy: Marker trait for types that can be cloned by moving byte-for-byte copy of memory containing the value + // Deref, DerefMut: Trait for smart pointers + // Default: types that have a sensible default value + // ToOwned: Conversion trait for converting a reference to an owned value + // From, Into: Conversion trait for converting from one type to another + // TryFrom, TryInto, Same as above, but might fail + // + + // Drop + // you can customize how run process dropping of types by implementing Drop for your type + + #[derive(Debug)] + struct Person { + name: String, + } + + impl Drop for Person { + fn drop(&mut self) { + println!("Dropping Person") + } + } + + let brian = Person { + name: "Brian".to_string(), + }; + println!("brian: {brian:?}"); + println!("person name: {:?}", brian.name); + // rust calls the drop trait of a type if it implement the Drop trait + // and then after that drops the value, so the value is not dropped in the drop + // method of the Drop trait but some other logic can happen there + + // if a variable value is moved into another variable, when the variable goes out of scope + // rust will not try to drop it, since it does not contain any value +}