Rust可以访问各种原语。一个示例包括:
i8, i16, i32, i64 和isize (指针大小)
u8, u16, u32, u64 和 usize (指针大小)
f32, f64
char Unicode标值一样 '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;
}