[][src]Function tao_of_rust::ch06::functions::function_pattern_match

pub fn function_pattern_match()

函数:函数参数模式匹配

Base usage:

#[derive(Debug)]
struct S { i: i32 }
fn f(ref _s: S) {
    println!("{:p}", _s); //0x7ffdd1364b80
}
fn main() {
    let s = S { i: 42 };
    f(s);
    // println!("{:?}", s);
}Run

Base usage: 使用通配符忽略参数

fn foo(_: i32) {
    // do something
}
fn main() {
    foo(3);
}Run

Base usage: 模式匹配解构元组

fn swap((x, y): (&str, i32)) -> (i32, &str){
    (y, x)
}
fn main() {
    let t = ("Alex", 18);
    let t = swap(t);
    assert_eq!(t, (18, "Alex"));
}Run