• 0 Posts
  • 11 Comments
Joined 1 year ago
cake
Cake day: July 23rd, 2023

help-circle
  • 404@lemmy.ziptoRust@programming.devstruct in Rust
    link
    fedilink
    English
    arrow-up
    7
    ·
    1 day ago

    You can implement Display for custom structs, to print them in the regular manner:

    use std::fmt;
    
    struct Point {
        x: i32,
        y: i32,
    }
    
    impl fmt::Display for Point {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(f, "({}, {})", self.x, self.y)
        }
    }
    
    fn main() {
        let point = Point { x: 10, y: 20 };
        println!("{}", point); // using standard println!
    }
    

    You can also implement things like Add, Sub, AddAssign (point_a += point_b)… :)