mirror of https://github.com/KaiserY/trpl-zh-cn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
705 B
34 lines
705 B
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 {}
|
|
|
|
// ANCHOR: here
|
|
use std::fmt;
|
|
|
|
impl fmt::Display for Point {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "({}, {})", self.x, self.y)
|
|
}
|
|
}
|
|
// ANCHOR_END: here
|
|
|
|
fn main() {
|
|
let p = Point { x: 1, y: 3 };
|
|
p.outline_print();
|
|
}
|