Rust的数据类型
文章目录
Scalar Types
Integer
| Length | Signed | Unsigned |
|---|---|---|
| 8-bit | i8 | u8 |
| 16-bit | i16 | u16 |
| 32-bit | i32 | u32 |
| 64-bit | i64 | u64 |
| 128-bit | i128 | u128 |
| arch | isize | usize |
signed: -$2^{n-1}$ to $2^{n-1}$-1
unsigned: 0 to $2^n$-1
PS. Integer Literals in Rust
| Number literals | Example |
|---|---|
| Decimal | 98_222 |
| Hex | 0xff |
| Octal | 0o77 |
| Binary | 0b1111_0000 |
Byte (u8 only) | b'A' |
Floating-Point
f32: 单精度。
f64: 双精度,rust里小数默认是f64。
Boolean
使用 bool 声明布尔类型,包括 true 和 false ,占一个 byte 大小。
Character
char 类型占四个 byte 大小。
Compound Types
Tuple
可以存放不同类型的数据,长度固定,一旦声明,将无法拓展或者压缩大小。
| |
| |
Array
里面的所有数据类型必须一致,长度和 Tuple 一样也是固定的,一旦声明将无法更改。
Array 里的数据存放在 stack 而不是 heap。
| |
| |
| |
评论正在加载中...如果评论较长时间无法加载,你可以 搜索对应的 issue 或者 新建一个 issue 。