如何判断变量在一个范围内
· One min read
C 家族语言:上界和下界的合取式
if (x >= 0 && x <= 10) {
// x is in range of [0, 10]
}
Python:不等式串
if 0 <= x <= 10:
# x is in range of [0, 10]
SQL:英语介词 between .. and
select x from table where x between 0 and 10
Kotlin:英语介词 in ..
if (x in 0..10) {
// x is in range of [0, 10]
}
Swift:初学者的模式匹配
if 0...10 ~= x {
// x is in range of [0, 10]
}
C#:模式匹配的合取式
if (x is >= 0 and <= 10) {
// x is in range of [0, 10]
}
Rust & Swift:模式匹配 Pro
Rust
if let 0..=10 = x {
// x is in range of [0, 10]
}
Swift
if case 0...10 = x {
// x is in range of [0, 10]
}