Skip to content

Instantly share code, notes, and snippets.

@tenthree
Last active September 5, 2018 09:47
Show Gist options
  • Save tenthree/1fb2798502de58c5e98f23b4c9d99180 to your computer and use it in GitHub Desktop.
Save tenthree/1fb2798502de58c5e98f23b4c9d99180 to your computer and use it in GitHub Desktop.
implement Display and Debug Trait with generics struct in Rust
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