Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/bin/20b_operator_overloading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ fn main() -> Result<(), Box<dyn Error>> {
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(())
}
39 changes: 39 additions & 0 deletions src/bin/41_utility_traits.rs
Original file line number Diff line number Diff line change
@@ -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
}