Rust原语 - Rust教程
Rust可以访问各种原语。一个示例包括:
- 有符号整数:
i8,i16,i32,i64和isize(指针大小) - 无符号整数:
u8,u16,u32,u64和usize(指针大小) - 浮点:
f32,f64 charUnicode标值一样'a','α'和'∞'(每4字节)bool以及true或false- 和单元类型
(), 其唯一的值也是() - 数组类似于
[1, 2, 3] - 元组类似于
(1, true)
变量是可以注释类型。数字可另外经由后缀或默认值。整数默认为 i32 ,浮点数到 f64.
fn main() {
// Variables can be type annotated.
let logical: bool = true;
let a_float: f64 = 1.0; // Regular annotation
let an_integer = 5i32; // Suffix annotation
// Or a default will be used.
let default_float = 3.0; // `f64`
let default_integer = 7; // `i32`
let mut mutable = 12; // Mutable `i32`.
// Error! The type of a variable can't be changed
mutable = true;
}