Last active
September 5, 2018 09:47
-
-
Save tenthree/1fb2798502de58c5e98f23b4c9d99180 to your computer and use it in GitHub Desktop.
implement Display and Debug Trait with generics struct in Rust
This file contains hidden or 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::fmt; | |
// #[derive(Debug)] | |
struct Point<T> { | |
x: T, | |
y: T, | |
} | |
impl <T> fmt::Debug for Point<T> where T: fmt::Display { | |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
write!(f, "[debug] Point at ({}, {})", self.x, self.y) | |
} | |
} | |
impl <T> fmt::Display for Point<T> where T: fmt::Display { | |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
write!(f, "[display] Point at ({}, {})", self.x, self.y) | |
} | |
} | |
fn main() { | |
let p: Point<i32> = Point{ x:30, y:8 }; | |
println!("{:#?}", p); | |
println!("{}", p); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment