锈:Vec Vec T进入 Vec T [英] Rust: Vec<Vec<T>> into Vec<T>

查看:51
本文介绍了锈:Vec Vec T进入 Vec T的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

let a = vec![ vec![1, 2], vec![3, 4], vec![5, 6] ];

如何将 a 中所有 Vec 中包含的所有值收集到一个 Vec 中?

How can I gather into a single Vec all the values contained in all the Vecs in a ?

推荐答案

您可以使用 flatten 运算符以移除向量的嵌套.

You can use the flatten operator to remove the nesting of the vectors.

以下示例取自链接.

let data = vec![vec![1, 2, 3, 4], vec![5, 6]];
let flattened = data.into_iter().flatten().collect::<Vec<u8>>();
assert_eq!(flattened, &[1, 2, 3, 4, 5, 6]);

这篇关于锈:Vec Vec T进入 Vec T的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆