Rust 数据结构

notice

Rust 还没到1.0,开发很快,请看官方文档

Structs

Rust 结构体可使用 struct Name { field1: T1, field2: T2 [, ...] } 的形式声明。 T1,T2 表示类型,实例化一个struct也用相似的语法,没有struct关键字,好比
Point { x: 1.0, y: 2.0 }
Rust 的结构体和C的很是相似,甚至内存布局也同样,因此能够从C程序读取Rust的struct。
使用mypoint.x的形式访问struct的值。函数

struct Point {
    x: f64,
    y: f64
}

若是struct的实例是可变的,此实例的字段也都是可变的。
好比布局

let mut mypoint = Point { x: 1.0, y: 1.0 }; // 可变
let origin = Point { x: 0.0, y: 0.0 }; // 不可变

mypoint.y += 1.0; // `mypoint`  是可变的,字段也是
origin.y += 1.0; // ERROR: assigning to immutable field

match 模式匹配也能够匹配struct,基本的语法是
Name { fieldname: pattern, ... }:code

match mypoint {
    Point { x: 0.0, y: yy } => println!("{}", yy),
    Point { x: xx,  y: yy } => println!("{} {}", xx, yy)
}

在对struct的模式匹配中,你可能不须要全部的字段,可使用 ..Name { field1, .. })忽略其余的字段。例如:内存

match mypoint {
    Point { x, .. } => println!("{}", x)
}

Enums 枚举

一个简单的枚举类型定义:开发

enum Direction {
    North,
    East,
    South,
    West
}

其中North 是 0, East 是 1, South 是 2, and West 是 3. 默认值是先前的值加一。
使用as 能够把North 转为int文档

println!( "{:?} => {}", North, North as int );

指定常量值:it

enum Color {
  Red = 0xff0000,
  Green = 0x00ff00,
  Blue = 0x0000ff
}

更复杂的状况:io

enum Shape {
    Circle(Point, f64),
    Rectangle(Point, Point)
}

Circle 包含了一个Point 结构体和一个f64,Rectangle包含了两个Point结构体。
声明定义了类型ShapeShape 指的是一个·Shape 类型外加两个函数CircleRectangle 用来构造该类型的值。
建立新的Circle,这样写Circle(Point { x: 0.0, y: 0.0 }, 10.0)
固然也能够进行模式匹配,访问枚举实例值的惟一方式是解构。例如:table

use std::f64;
fn area(sh: Shape) -> f64 {
    match sh {
        Circle(_, size) => f64::consts::PI * size * size,
        Rectangle(Point { x, y }, Point { x: x2, y: y2 }) => (x2 - x) * (y2 - y)
    }
}

使用_忽略个别字段,忽略全部的字段使用Circle(..)
枚举的模式匹配:ast

fn point_from_direction(dir: Direction) -> Point {
    match dir {
        North => Point { x:  0.0, y:  1.0 },
        East  => Point { x:  1.0, y:  0.0 },
        South => Point { x:  0.0, y: -1.0 },
        West  => Point { x: -1.0, y:  0.0 }
    }
}

枚举变量也能够是struct,例如:

use std::f64;
enum Shape {
    Circle { center: Point, radius: f64 },
    Rectangle { top_left: Point, bottom_right: Point }
}
fn area(sh: Shape) -> f64 {
    match sh {
        Circle { radius: radius, .. } => f64::consts::PI * square(radius),
        Rectangle { top_left: top_left, bottom_right: bottom_right } => {
            (bottom_right.x - top_left.x) * (top_left.y - bottom_right.y)
        }
    }
}

Tuples 元组

元组(Tuples)有点相似struct,不过元组的字段是没有名字的。你也不能经过点来访问字段。
元组能够有任意个元素,没有0个元素的状况(() 若是你喜欢,能够看成空元组)。

let mytup: (int, int, f64) = (10, 20, 30.0);
match mytup {
  (a, b, c) => info!("{}", a + b + (c as int))
}

Tuple structs

Rust 提供了tuple structs,元组和结构体的结合。tuple structs 是有名字的(Foo(1, 2)的类型和Bar(1, 2)的不同)。tuple structs的字段没有名字。

例如:

struct MyTup(int, int, f64);
let mytup: MyTup = MyTup(10, 20, 30.0);
match mytup {
  MyTup(a, b, c) => info!("{}", a + b + (c as int))
}

有个特例是只有一个字段的tuple structs ,能够称做newtypes (相似Haskell里的"newtype"特性)。用来建立新的类型,而不是一种类型的别名而已。

struct GizmoId(int);

像这种类型定义对于区分基础类型同样,可是用途不同的数据是颇有用的。

struct Inches(int);
struct Centimeters(int);

上边的定义能够以一个简单的方式避免混淆不一样单元的数字。整型值能够用模式匹配:

let length_with_unit = Inches(10);
let Inches(integer_length) = length_with_unit;
println!("length is {} inches", integer_length);
相关文章
相关标签/搜索