MST

星途 面试题库

面试题:Rust中std::fmt traits自定义格式化之基础应用

请实现一个结构体`Point`,包含`x`和`y`两个`i32`类型的字段。使用`std::fmt::Display` trait 对其进行格式化,要求输出格式为`(x, y)`,例如`(3, 5)`。请写出完整的代码实现。
43.5万 热度难度
编程语言Rust

知识考点

AI 面试

面试题答案

一键面试
struct Point {
    x: i32,
    y: i32,
}

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