< 返回版块

“and the rest” pattern binding

Mike Tang 发表于

看这里,

https://doc.rust-lang.org/book/second-edition/appendix-02-operators.html

.. (variant(x, ..), struct_type { x, .. }): “and the rest” pattern binding.

这句话什么意思,怎么用?

评论区

CrLF0710 2018-03-05T07:49:07.059666
pub struct Point {
    pub x: i32,
    pub y: i32,
}

fn main() {
    let pt = Point {x: 4, y: 5};
    
    println!("{}", match pt {
    Point {x, .. } => x
    });
}
codeworm96 2018-03-05T07:49:23.715200
作者 Mike Tang 2018-03-05T07:53:44.360919

原来是用在match里面。

@CrLF0710 pub struct Point { pub x: i32, pub y: i32, }

fn main() { let pt = Point {x: 4, y: 5};

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

}

1 共 3 评论, 共 1 页