通过例子学习Rust

16.2 解构结构体

A struct can be destructured with pattern matching.

fn main() { struct Foo { x: (uint, uint), y: uint } // destructure members of the struct let foo = Foo { x: (1, 2), y: 3 }; let Foo { x: (a, b), y } = foo; println!("a = {}, b = {}, y = {} ", a, b, y); // you can destructure structs and rename the variables, // the order is not important let Foo { y: i, x: j } = foo; println!("i = {}, j = {}", i, j); // and you can also ignore some variables: let Foo { y, .. } = foo; println!("y = {}", y); // this will give an error: pattern does not mention field `x` // let Foo { y } = foo; }