Created
April 23, 2022 02:35
-
-
Save uztbt/7df0d4a648b4907960c10092780ac3ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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