Rust Tips 比较数值
内容
- 比较与类型转换
- 浮点类型比较
可以用这些运算符比较数值
> < == != >= <=
无法比较不同类型的值
fn main() {
let a: i32 = 10;
let b: u16 = 100;
if a < b { // 报错 mismatched types
println!("Ten is less than one hundred.");
}
}
解决办法 1:使用 as 进行类型转换
fn main() {
let a: i32 = 10;
let b: u16 = 100;
if a < (b as i32) {
println!("Ten is less than one hundred.");
}
}
注意:从比较小的类型转成比较大的类型通常是比较安全的
fn main() {
let a: i32 = 10;
let b: u16 = 100;
if a < (b as i32) {
println!("10 is less than 100.")
}
let c : i32 = 1203414;
println!("{}", c as i8);
}
运行
rust_compare_numerical_values on master [?] is
标签:compare,via,0.1,numerical,数值,values,Rust,Tips,rust
From: https://www.cnblogs.com/QiaoPengjun/p/17436557.html