Rust編程進階:060、全部模式語法3

例子1:git

// 解構並分解值
// 解構元祖、結構體、枚舉、引用
//
// 解構結構體
struct Point {
    x: i32,
    y: i32,
}

fn main() {
    // let p = Point{x: 1, y: 2};
    // let Point{x: a, y: b} = p; // 變量a和變量b匹配x和y
    // assert_eq!(1, a);
    // assert_eq!(2, b);
    //
    // let Point{x, y} = p;
    // assert_eq!(1, x);
    // assert_eq!(2, y);

    let p = Point { x: 1, y: 0 };
    match p {
        Point { x, y: 0 } => println!("x axis"),
        Point { x: 0, y } => println!("y axis"),
        Point { x, y } => println!("other"),
    };
}

例子2:github

enum Message {
    Quit,
    Move { x: i32, y: i32 },
    Write(String),
    ChangeColor(i32, i32, i32),
}

fn main() {
    let msg = Message::ChangeColor(0, 160, 255);
    match msg {
        Message::Quit => {
            println!("quit");
        }
        Message::Move { x, y } => {
            println!("move, x: {}, y: {}", x, y);
        }
        Message::Write(text) => println!("write msg: {}", text),
        Message::ChangeColor(r, g, b) => {
            println!("clolor, r: {}, g: {}, b: {}", r, g, b);
        }
    };
}

例子3:ui

enum Message {
    Quit,
    Move { x: i32, y: i32 },
    Write(String),
    Change(i32, i32, i32),
}

// 等同於
// struct QuitMessage; // 類單元結構體
// struct MoveMessage {
//   x: i32,
//   y: i32,
// }
// struct WriteMessage(String)
// struct Change(i32, i32, i32)

impl Message {
    fn prin(&self) {
        match self {
            Message::Quit => println!("Quit"),
            Message::Move { x, y } => println!("Move x = {}, y = {}", x, y),
            Message::Change(a, b, c) => println!("Change a = {}, b = {}, c = {}", a, b, c),
            // _ => println!("Write")
            Message::Write(s) => println!("Write = {}", s),
        }
    }
}

fn main() {
    let quit = Message::Quit;
    quit.prin();

    let mo = Message::Move { x: 10, y: 20 };
    mo.prin();

    let wri = Message::Write(String::from("Hello"));
    wri.prin();

    let change = Message::Change(1, 2, 3);
    change.prin();
}

例子4:url

enum Color {
    Rgb(i32, i32, i32),
    Hsv(i32, i32, i32),
}

enum Message {
    Quit,
    Move { x: i32, y: i32 },
    Write(String),
    ChangeColor(Color),
}

fn main() {
    let msg = Message::ChangeColor(Color::Hsv(0, 160, 255));
    match msg {
        Message::ChangeColor(Color::Rgb(r, g, b)) => {
            println!("rgb color, r: {}, g: {}, b: {}", r, g, b);
        }
        Message::ChangeColor(Color::Hsv(h, s, v)) => {
            println!("hsv color, h: {}, s: {}, v: {}", h, s, v);
        }
        _ => (),
    };

    println!("Hello, world!");
}

例子5:.net

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

fn main() {
    let ((a, b), Point { x, y }) = ((1, 2), Point { x: 3, y: 4 });
    println!("a: {}, b: {}, x: {}, y: {}", a, b, x, y);
    println!("Hello, world!");
}

本節所有源代碼:
https://github.com/anonymousGiga/learn_rust/blob/master/learn_pattern3/src/main.rs
https://github.com/anonymousGiga/learn_rust/blob/master/learn_pattern4/src/main.rs
https://github.com/anonymousGiga/learn_rust/blob/master/learn_enum2/src/main.rs
https://github.com/anonymousGiga/learn_rust/blob/master/learn_pattern5/src/main.rs
https://github.com/anonymousGiga/learn_rust/blob/master/learn_pattern6/src/main.rscode

相關文章
相關標籤/搜索