首先定义一个判断类型的函数:
fn get_type<T>(_: &T) -> &str {
std::any::type_name::<T>()
}
关于这个函数的详细信息后面章节再做解释,现在你需要清楚它可以返回某个值的类型。
在 Rust 中默认的整数数字类型为 i32
:
let num1 = 123;
println!("{} - type: {}", num1, get_type(&num1)); // 123 - type: i32
默认的浮点数字类型为 f64
:
let num2 = 1.23;
println!("{} - type: {}", num2, get_type(&num2)); // 1.23 - type: f64
你可以显式定义某个值的类型:
let num3: i8 = 23;
println!("{} - type: {}", num3, get_type(&num3)); // 23 - type: i8
最大值
std
是标准库(crate),提供了丰富多样的功能,这里我们使用类型模块(i32、i16 等) 和属性。
let max_i32 = i32::MAX;
let max_i16 = i16::MAX;
println!("max value for i32 is {}", max_i32); // max value for i32 is 2147483647
println!("max value for i16 is {}", max_i16); // max value for i16 is 32767
布尔值
let is_rust_fun: bool = true;
println!(
"is_rust_fun is {} - type: {}",
is_rust_fun,
get_type(&is_rust_fun)
); // is_rust_fun is true - type: bool
let is_greater = 23 > 5;
println!(
"is_greater is {} - type: {}",
is_greater,
get_type(&is_greater)
); // is_greater is true - type: bool
布尔值运算
定义两个布尔值:
let a = true;
let b = false;
println!("a is {}\nb is {}", a, b);
// a is true
// b is false
使用 !
取反
println!("NOT a is {}", !a); // NOT a is false
使用 &
按位与,使用 &&
逻辑与
println!("a AND b is {}", a & b); // a AND b is false
使用 |
按位或,使用 ||
逻辑或
println!("a OR b is {}", a | b); // a OR b is true
使用 ^
XOR 按位异或运算
println!("a XOR b is {}", a ^ b); // a XOR b is true
布尔类型可以映射到整数类型的 1 或 2
println!("a XOR b is {}", (a ^ b) as i32); // a XOR b is 1
let c = (a ^ b) | (a & b);
println!("c is {}", c); // c is true
短路逻辑操作,忽略右操作数
let d = true || (a & b);
println!("d is {}", d); // d is true
panics
是一种错误处理方式,它会抛出错误栈并立即退出程序,类似于 Node.js 中的 throwing error
。
let e = false && panic!();
println!("e is {}", e); // e is false
由于 panic!()
没有被执行,所以程序执行打印并正常退出。
chars 字符
chars 字符使用4 个字节(32位)存储:
let letter: char = 'z';
let number_char = '9';
let finger = '\u{261D}';
println!("letter is {}", letter); // letter is z
println!("number_char is {}", number_char); // number_char is 9
println!("finger is {}", finger); // finger is ☝
let smiley = '😀';
println!("smiley is {} - type: {}", smiley, get_type(&smiley));
// smiley is 😀 - type: char
浮点数
- f32 32 位浮点数
- f64 64 位浮点数
默认小数存储在 f64
中:
let my_float = 12.345677890123456789012345;
println!("my_float is: {}", my_float);
// my_float is: 12.345677890123456
let a_float: f32 = 9.9438535983578493758;
println!("a_float is: {}", a_float);
// a_float is: 9.943853
let min_f32 = f32::MIN;
println!("min_f32 is: {}\n", min_f32);
// min_f32 is: -340282350000000000000000000000000000000
let max_f32 = f32::MAX;
println!("max_f32 is: {}\n", max_f32);
// max_f32 is: 340282350000000000000000000000000000000
let min_f64 = f64::MIN;
println!("min_f64 is: {}\n", min_f64);
// min_f64 is: -179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
let max_f64 = f64::MAX;
println!("max_f64 is: {}\n", max_f64);
// max_f64 is: 179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000