美文网首页
rust模式匹配

rust模式匹配

作者: Wu杰语 | 来源:发表于2024-05-06 21:39 被阅读0次

    1. 模式匹配的例子

    模式匹配在函数式范式中使用的比较广泛,学rust的模式匹配,不妨先看看go和erlang语言中模式匹配的小例子。

    1.1 go语言Pattern

    go语言模式匹配包括数组、切片、字符串、结构体,一个数组的例子如下

    arr := [3]int{1, 2, 3}
    switch arr {
        case [3]int{1, 2, 3}:
            fmt.Println("Matched array pattern")
        default:
            fmt.Println("No match")
    }
    

    代码会直接匹配到第一个case上。

    1.2 erlang语言Pattern

    erlang模式匹配包括元组、列表、二进制、位串等

    List = [1, 2, 3].
    case List of
        [1, 2, 3] ->
            io:format("Matched list pattern~n");
        _ ->
            io:format("No match~n")
    end.
    

    2. rust语言Pattern

    rust语言模式匹配和go、erlang如出一辙,主要是在语法表述上有所不同,包括整型、字符串、变量、数组、结构体、元组、数组、切片;

    2.1 整型Pattern

    for i in -2..5 {
        match i {
            -1 => println!("It's minus one"),
            1 => println!("It's a one"),
            2|4 => println!("It's either a two or a four"),
            _ => println!("Matched none of the arms"),
        }
    }
    

    rust中使用的关键字是match,每个分支使用=>符号,其中最后一个分支"_"表示其余,和erlang语言的表达方式是一样的。

    2.2 字符串Pattern

    let str = "hello";
    match str {
        "hello" => println!("Matched string pattern"),
        _ => println!("No match"),
    }
    

    2.3 数组或切片

    // Fixed size
    let arr = [1, 2, 3];
    match arr {
        [1, _, _] => "starts with one",
        [a, b, c] => "starts with something else",
    };
    

    这是数组的Pattern,可以匹配到1开头的数组,或者非1开头的数组

    // Dynamic size
    let v = vec![1, 2, 3];
    match v[..] {
        [a, b] => { /* this arm will not apply because the length doesn't match */ }
        [a, b, c] => { /* this arm will apply */ }
        _ => { /* this wildcard is required, since the length is not known statically */ }
    };
    

    这个是切片的Pattern,和数组的匹配类似,但是对于数组来说,第二个匹配会有Move语义,如果每个元素没有完成Cop语义,会导致匹配完成后数组元素不可用。

    2.3.1 rest

    erlang中使用[H|T] = [1,2,3],H表达第一个元素,T表达其它元素,在rust中怎么表达呢,如下例子:

    match slice {
        [] => println!("slice is empty"),
        [one] => println!("single element {}", one),
        [head, tail @ ..] => println!("head={} tail={:?}", head, tail),
    }
    

    这里使用了..表达一个或者多个变量,而tail @ ..是起到绑定作用,将..代表的值绑定到tail。

    2.3.1 identify Pattern

    如上tail就是idntify Pattern,其表示形式为 IDENTIFIER (@ PatternNoTopAlt ) ?,一个例子如下

    let x = 2;
    match x {
        e @ 1 ..= 5 => println!("got a range element {}", e),
        _ => println!("anything"),
    }
    

    第一个case的@后面是PatternNoTopAlt,表示一个range,从1到5的数字,如果满足,则将x赋值给e。

    2.4 结构体匹配

    结构体匹配一个例子如下

    match s {
        Point {x: 10, y: 20} => (),
        Point {y: 10, x: 20} => (),    // order doesn't matter
        Point {x: 10, ..} => (),
        Point {..} => (),
    }
    

    还有比较典型的枚举Result,异常的匹配

    match result {
      Ok(v) => v,
      Err(e) => return Err(e.into())
    }
    

    对于结构的match,在绑定值的时候会有Move的语义,如果被Move的值没有实现Copy语义,就需要使用ref关键字,表达借用

    match a {
        None => (),
        Some(value) => (), // value会获取值的所有权
    }
    
    match a {
        None => (),
        Some(ref value) => (),  // valu表达了借用,不会移动a中Some(T)的T值。
    }
    

    小结

    模式匹配在rust中使用非常广泛,包括rust的宏也使用了模式匹配。rust和go语言、erlang语言的模式匹配,想解决的问题是一致的,只是所使用的语法语义表达上有区别,知晓这种区别,学习rust模式匹配就轻车熟路了。

    相关文章

      网友评论

          本文标题:rust模式匹配

          本文链接:https://www.haomeiwen.com/subject/zqalfjtx.html