作者:Sunny-阿坚 | 来源:互联网 | 2023-08-25 16:49
析构结构–structs同样,一个结构可以解构如图所示:fnmain(){structFoo{x:(u32,u32),y:u32}destructuremembersofthest
析构结构 – structs
同样,一个结构可以解构如图所示:
fn main() {
struct Foo { x: (u32, u32), y: u32 }
// 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;
}
也可以看看:
结构体, 引用模式