Skip to content

Instantly share code, notes, and snippets.

@uztbt
Created April 23, 2022 02:35
Show Gist options
  • Save uztbt/7df0d4a648b4907960c10092780ac3ce to your computer and use it in GitHub Desktop.
Save uztbt/7df0d4a648b4907960c10092780ac3ce to your computer and use it in GitHub Desktop.
use std::ops::Deref;
struct CustomSmartPointer {
data: String,
}
impl Deref for CustomSmartPointer {
type Target = String;
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl Drop for CustomSmartPointer {
fn drop(&mut self) {
println!("Dropping CustomSmartPointer with data {}", self.data)
}
}
fn add_bar(s: &str) -> String {
let mut ans = String::from(s);
ans.push_str("Bar");
ans
}
fn main() {
let c = CustomSmartPointer {
data: String::from("Jiayi"),
};
// Deref coersion happens for function calls
add_bar(&c);
// However, deref coersion DON"T happen for macro calls
println!("Finishing main function! {}", &c);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment