The problem of printing more parentheses for OutlinePrint examples in the book rust programming language

The example code for the chapter

Advanced trait has an extra parenthesis at the end. How strange! Rust"s bug?

**********
*        *
* (1, 3) *
*        *
**********

rust online running link

related codes


use std::fmt;

trait OutlinePrint: fmt::Display {
    fn outline_print(&self) {
        let output = self.to_string();
        let len = output.len();
        println!("{}", "*".repeat(len + 4));
        println!("*{}*", " ".repeat(len + 2));
        println!("* {} *", output);
        println!("*{}*", " ".repeat(len + 2));
        println!("{}", "*".repeat(len + 4));
    }
}

struct Point {
    x: i32,
    y: i32,
}

impl OutlinePrint for Point {}

impl fmt::Display for Point {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "({}, {})", self.x, self.y)
    }
}

fn main() {
    let p = Point{x:1, y:3};

    println!("{:?}", p.outline_print());

}
Jul.05,2022
  • Why dereferencing enum causes move?

    Code of error: match *self { ... } I don t quite know the difference between the two. Why does the former cause move or copy?? ...

    Feb.28,2021
  • On the problem of website optimization

    A personal blog-like website developed using javaweb, the home page needs to load a lot of things, such as articles, mood quotes similar to Weibo, latest comments, announcements, and broadcast pictures. I originally wanted to use freemarker, but I found ...

    Mar.13,2021
  • The working Mechanism of match in Rust language

    Rust is a novice rookie. I began to learn this young language just a few days ago, and I have been learning it for almost half a week. I have to say that there are many things I don t quite understand about the syntax of Rust, and the threshold for en...

    Jan.02,2022
Menu