在Rust中,一个类型实现了Display trait,则这个类型的变量就能够转换成字符串形式。在风格化输出信息时,还是很有用的。下面是定义:
pub trait Display {
fn fmt(&self, f: &mut Formatter<'_>) -> Result;
}
下面是实例代码:
use std::fmt;
struct Point(isize, isize);
// 实现OutlinePrint的前提是实现了fmt::Display
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));
}
}
impl fmt::Display for Point {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({} {})", self.0, self.1)
}
}
impl OutlinePrint for Point {}
fn main() {
let p = Point(1, 2);
p.outline_print();
}
输出:
*********
* *
* (1 2) *
* *
*********