rust笔记二

http://doc.rust-lang.org/0.12.0/guide.html

8复合类型
tuples元组:有序,固定大小

let x = (1i, "hello");
1i看起来像li,~~~~~日,或
let x: (int, &str) = (1, "hello");
&str读作a string slice,可以看到tuple的类型也像一个tuple,只不过类型中给的是类型名,而右值给的是值

let可将一个tuple打散。。。
let (x, y, z) = (1i, 2i, 3i);

println!("x is {}", x);
通过tuple实现多值返回(其实就是返回了一个tuple)
fn next_two(x: int) -> (int, int) { (x + 1i, x + 2i) }

fn main() {
    let (x, y) = next_two(5i);
    println!("x, y = {}, {}", x, y);
} 

结构体struct
我觉得和javascript中的对象literal一模一样
和tuple的区别,tuple用的(),struct用的{}, tuple无需关键字,struct要多写两个单词,struct每种field有个名字, 无序, 使用let mut以便定义的struct数据成员可以修改

struct Point {
    x: int,
    y: int,
}

fn main() {
    let origin = Point { x: 0i, y: 0i };

    println!("The origin is at ({}, {})", origin.x, origin.y);
}
可修改版:
struct Point {
    x: int,
    y: int,
}

fn main() {
    let mut point = Point { x: 0i, y: 0i };

    point.x = 5;

    println!("The point is at ({}, {})", point.x, point.y);
}
发现和javascript对象字面量不一样的地方是,最后一个变量定义后面有个,号!!!!这是一个进步

8.3 tuple struct
定义:和struct的区别,field没用名字,最后一个field后没有多余的,号,{}变成 了()

struct Color(int, int, int);
struct Point(int, int, int);
使用 {}变成了(), field无需名字:
let black  = Color(0, 0, 0);
let origin = Point(0, 0, 0);


以上的定义用struct来实现

struct Color {
    red: int,
    blue: int,
    green: int,
}

struct Point {
    x: int,
    y: int,
    z: int,
}
用struct写的代码更易懂(名字比位置更清楚),所以 少用tuple struct,

只包含一个元素的tuple struct称为newtype,完全不懂有什么特别之处
通过let可以打散newtype
struct Inches(int);

let length = Inches(10);

let Inches(integer_length) = length;
println!("length is {} inches", integer_length);

enum枚举(和java一样)
enum Ordering {
    Less,
    Equal,
    Greater,
}  
用法:
fn cmp(a: int, b: int) -> Ordering {
    if a < b { Less }
    else if a > b { Greater }
    else { Equal }
}

fn main() {
    let x = 5i;
    let y = 10i;

    let ordering = cmp(x, y);

    if ordering == Less {
        println!("less");
    } else if ordering == Greater {
        println!("greater");
    } else if ordering == Equal {
        println!("equal");
    }
}
带参数的enum
enum OptionalInt {
    Value(int),
    Missing,
}

fn main() {
    let x = Value(5);
    let y = Missing;

    match x {
        Value(n) => println!("x is {:d}", n),
        Missing  => println!("x is missing!"),
    }

    match y {
        Value(n) => println!("y is {:d}", n),
        Missing  => println!("y is missing!"),
    }
}

match(告别if else if)
let x = 5i;

match x {
    1 => println!("one"),
    2 => println!("two"),
    3 => println!("three"),
    4 => println!("four"),
    5 => println!("five"),
    _ => println!("something else"),
}  
另一个例子:
fn cmp(a: int, b: int) -> Ordering {
    if a < b { Less }
    else if a > b { Greater }
    else { Equal }
}

fn main() {
    let x = 5i;
    let y = 10i;

    match cmp(x, y) {
        Less    => println!("less"),
        Greater => println!("greater"),
        Equal   => println!("equal"),
    }
}
match也是一个表达式:
fn cmp(a: int, b: int) -> Ordering {
    if a < b { Less }
    else if a > b { Greater }
    else { Equal }
}

fn main() {
    let x = 5i;
    let y = 10i;

    let result = match cmp(x, y) {
        Less    => "less",
        Greater => "greater",
        Equal   => "equal",
    };

    println!("{}", result);
}  



end!


你可能感兴趣的:(rust笔记二)