// 自定义错误类型
#[derive(Debug)]
struct PointError;
// Point结构体
struct Point {
x: i32,
y: i32,
}
// 检查x和y是否相等的函数
fn check_point(p: Point) -> Result<(), PointError> {
match p {
Point { x, y } if x == y => Ok(()),
_ => Err(PointError),
}
}
fn main() {
let point1 = Point { x: 5, y: 5 };
let point2 = Point { x: 5, y: 10 };
match check_point(point1) {
Ok(()) => println!("The x and y values of point1 are equal."),
Err(e) => println!("Error in point1: {:?}", e),
}
match check_point(point2) {
Ok(()) => println!("The x and y values of point2 are equal."),
Err(e) => println!("Error in point2: {:?}", e),
}
}