我可以通过获取向量切片并引用元组中的项目来解构元组向量:
let items = vec![("Peter".to_string(), 180)];
if let [(ref name, ref age)] = items.as_slice() {
println!("{} scored {}", name, age);
};
如何直接解构向量,将项目移出元组。像这样的东西:
let items = vec![("Peter".to_string(), 180)];
if let [(name, age)] = items {
println!("{} scored {}", name, age);
};
编译上述导致错误:
error[E0529]: expected an array or slice, found `std::vec::Vec<(std::string::String, {integer})>`
--> src/main.rs:4:12
|
4 | if let [(name, age)] = items {
| ^^^^^^^^^^^^^ pattern cannot match with input type `std::vec::Vec<(std::string::String, {integer})>`