Skip to content

Instantly share code, notes, and snippets.

@syusui-s
Created May 28, 2017 02:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syusui-s/caef481977731313d960e9631e06af90 to your computer and use it in GitHub Desktop.
Save syusui-s/caef481977731313d960e9631e06af90 to your computer and use it in GitHub Desktop.
use std::ops::Deref;
struct DerefExample<T> {
value: T
}
impl<T> Deref for DerefExample<T> {
type Target = T;
fn deref(&self) -> &T {
&self.value
}
}
struct NonDeref<T> {
value: T
}
fn main() {
let x = DerefExample{ value: "abc" };
let s1 : String = x.to_owned();
let y = NonDeref{ value: "abc" };
// let s2 : String = y.to_owned(); // <- エラー発生
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment