发布于 2015-07-16 14:17:53 | 528 次阅读 | 评论: 0 | 来源: 网络整理

Rust可以访问各种原语。一个示例包括:

  • 有符号整数: i8i16i32i64 和isize (指针大小)
  • 无符号整数: u8u16u32u64 和 usize (指针大小)
  • 浮点: f32f64
  • 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;
}

另请参见:

std库

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务