简体   繁体   中英

Using rust how can I convert a 2d array into a 2d vector?

Given a 2d array such as

let S = [
        [0,0,1,0,0],
        [0,0,1,0,0],
        [0,0,1,0,0],
        [0,0,1,0,0]];

How can I convert it into a 2d Vector?

Vec<Vec<i32>>

2d array is array of array s, so to create 2d Vector you need to create Vec of Vec s. One way to do that is to iterate over the array and convert every item using to_vec() method, then collect() to Vec .

let s: Vec<_> = s.iter().map(|&e| e.to_vec()).collect();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM