编写一系列浮点范围比较的最佳方法是什么?要使用下面GitHub评论中的示例,
let color = match foo {
0.0...0.1 => Color::Red,
0.1...0.4 => Color::Yellow,
0.4...0.8 => Color::Blue,
_ => Color::Grey,
};
Run Code Online (Sandbox Code Playgroud)
天真的解决方案将是一个痛苦的if-else链:
let color = {
if 0.0 <= foo && foo < 0.1 {
Color::Red
}
else if 0.1 <= foo && foo < 0.4 {
Color::Yellow
}
else if 0.4 <= foo && foo < 0.8 {
Color:: Blue
}
else {
Color::Grey
}
}
Run Code Online (Sandbox Code Playgroud)
这真的是最好的选择吗?必须有一个更好的方式来写这个,对吧?
与匹配浮点的替代相关,但这是用于范围比较.
本来中提到的跟踪问题的illegal_floating_point_literal_pattern
,有事我运行到不断.
rust ×1