Rust 基础数据类型
直接查看附录 Rust 基础类型表,只记录 Rust 与 Java 之间更特别的地方.差不多的地方将被一笔带过.
固定宽度的数值类型
直接查看附录 Rust 数值类型
整型
直接查看附录 Rust 数值类型
检查算法
直接查看附录 运算名称
以下代码可以帮助你检查对应内容
assert_eq!(10_u8.checked_add(20),Some(30))
浮点类型
let f: f32 = 0.1_f32;
std::f32::consts或std::f64::consts提供了一些数学常量E,PI,2的平方根
布尔类型
- true
- false
false 可以转换为整型 0
assert_eq!(false as i32, 0);
字符
assert_eq!('*'.is_alphabetic(), false);
assert_eq!('β'.is_alphabetic(), true);
assert_eq!('8'.to_digit(10), Some(8));
assert_eq!('ಠ'.len_utf8(), 3);
assert_eq!(std::char::from_digit(2, 10), Some('2'));
is_alphabetic可以检查是否为字母
元组
Rust代码通常会用元组类型从一个函数返回多个值。
每个元素赋值给不同的变量:
let text = "I see the eigenvalue in thine eye";
let (head, tail) = text.split_at(21);
assert_eq!(head, "I see the eigenvalue ");
assert_eq!(tail, "in thine eye");
这样比其等效写法更易读:
let text = "I see the eigenvalue in thine eye";
let temp = text.split_at(21);
let head = temp.0;
let tail = temp.1;
assert_eq!(head, "I see the eigenvalue ");
assert_eq!(tail, "in thine eye");
指针类型
- 引用
- Box
- 不安全指针
引用
Rust 引用有两种形式
&T不可变引用&mut T可变引用
Rust 的对象只有一个拥有者
Box
在堆中分配值的最简单方式是使用 Box::new:
let t = (12, "eggs");
let b = Box::new(t); // 在堆中分配一个元组
裸指针
在 Rust 中这是一个高级语言特性中的可选机制,其安全性取决于自己.
数组 向量 切片
数组
固定长度的数组,他是在栈上分配的
let arrars: [String; 3] = ["hello".to_string(), "world".to_string(), "rust".to_string()];
向量
不固定长度的数组,他是在堆上分配的
let array: Vec<String> = Vec::new();
切片
fn main() {
assert_eq!(10_u8.checked_add(20), Some(30));
let mut array: Vec<String> = Vec::new();
array.push("Hello".to_string());
array.push("World".to_string());
//此处做了一个切片操作,切片操作不会改变原数组的值
let array2 = &array[1..2];
println!("{:?}", array2);
}
字符串类型
let hello_world = "Hello World";
println!("{}", hello_world.to_uppercase());
附录
Rust 基础类型表
| 类型 | 说明 | 值 |
|---|---|---|
| i8,i16,i32,i64,i128 u8,u16,u32,u64,u128 |
给定位宽的有符号整数和无符号整数 | 42,-5i8,0x400u16,0o100i16,20_922_789_888_000u64 |
| isize,usize | 给机器字一样大的有符号整数和无符号整数 | 137,-0b0101_00010isize,0xffff_fc0010usize |
| f32,f64 | 单精度 IEEE 浮点数字和双精度 IEEE 浮点数字 | 1.61803,3.14f32.6.0221e23f64 |
| bool | 布尔值 | true,false |
| char | 32位宽(4 字节) | '*','\n','字' |
| (char,u8,i32) | 元组 | ('%','0x7f','-1') |
| () | 空元组 | () |
| struct S {x: f32,y: f32} | 具名字段型结构体 | S { x : 100.0 , y : 200.0 } |
| struct T (i32,char) | 元组型结构体 | T (120,'X') |
| struct E | 单元型结构体,无字段 | E |
| enum Attend {OnTime,Late(u32)} | 枚举 | Attend::Late(5) |
| Box |
Box: 指向堆中值的拥有型指针 | Box::new(Late(15)) |
| &i32,&mut i32 | 共享引用和可变引用: | &s.y &mut v |
| String | UTF-8 字符串,动态分配大小 | "Hello World","你好,世界" |
| &str | 对 str 的引用,指向 UTF-8 文本的非拥有型指针 | " そば : soba"、&s[0..12] |
| [f64;4],[u8;256] | 数组固定长度 | [1.0, 0.0, 0.0, 1.0]、[b' '; 256] |
| Vec |
向量,可变长度 | vec![0.367, 2.718, 7.389] |
| &[u8],&mut [8] | 对切片的引用,包含指针长度 | &v[10..20]、&mut a[..] |
| Option<&str> | 可选值 | Some("Dr."), None |
| Result<u64,Error> | 可能失败的操作结果:或者为成功值 Ok(v),或者为错误值 Err(e) | Ok(4096), Err(Error::last_os_error()) |
| &dyn Any、&mut dyn Read | 特型对象,是对任何实现了一组给定方法的值的引用 | value as &dyn Any、&mut file as &mut dyn Read |
| fn(&str) -> bool | 函数指针 | str::is_empty |
| (闭包类型没有显式书写形式) | 闭包 | ` |
Rust 数值类型
| 大小(位) | 无符号整数 | 有符号整数 | 浮点数 |
|---|---|---|---|
| 8 | u8 |
i8 |
|
| 16 | u18 |
i16 |
|
| 32 | u32 |
i32 |
f32 |
| 64 | u64 |
i64 |
64 |
| 128 | u128 |
i128 |
|
| 机器字 | usize |
isize |
Rust无符号类型范围
| 类型 | 范围 |
|---|---|
u8 |
0~255 |
u16 |
0~65535 |
u32 |
0~4294967295 |
u64 |
0~1844京 |
u128 |
3.4x10^38^ |
usize |
0 |
Rust有符号整型
| 类型 | 范围 |
|---|---|
i8 |
-128~127 |
i16 |
-32768~32767 |
i32 |
-2147483648~2147483647 |
i64 |
-9 223372036854775808~9223372036854775807 |
i128 |
-1.7×10^38^ ~ 1.7×10^38^ |
isize |
-2^31^ 到 2^31^-1 或 -2^63^ 到 2^63^-1 |
代替符号字符
| 字符 | 字节字面量 | 等效的数值 |
|---|---|---|
单引号(') |
b'\'' |
39u8 |
反斜杠(\) |
b'\\' |
92u8 |
换行(lf) |
b'\n' |
10u8 |
回车(cr) |
b'\r |
13u8 |
制表符(tab) |
b'\t' |
9u8 |
运算名称
| 运算 | 后缀名称 | 例子 |
|---|---|---|
| 加法 | add |
100_i8.checked_add(27) == Some(127) |
| 减法 | sub |
10_u8.checked_sub(11) == None |
| 乘法 | mul |
128_u8.saturating_mul(3) == 255 |
| 除法 | div |
64_u16.wrapping_div(8) == 8 |
| 求余 | rem |
(-32768_i16).wrapping_rem(-1) == 0 |
| 取负 | neg |
(-128_i8).checked_neg() == None |
| 绝对值 | abs |
(-32768_i16).wrapping_abs() == -32768 |
| 求幂 | pow |
3_u8.checked_pow(4) == Some(81) |
| 位左移 | shl |
10_u32.wrapping_shl(34) == 40 |
| 位右移 | shr |
40_u64.wrapping_shr(66) == 10 |
浮点类型
| 类型 | 精度 | 范围 |
|---|---|---|
f32 |
IEEE 单精度(至少 6 位小数) | 大约 -3.4 × 1038 至 +3.4 × 1038 |
f64 |
IEEE 双精度(至少 15 位小数) | 大约 -1.8 × 10308 至 +1.8 × 10308 |
评论