How to create and initialize a 2d vector of characters with different row lengths in Rust

1 Answer

0 votes
fn main() {
    // Initialize a 2D array with different row lengths
    let char_vec_2d: Vec<Vec<char>> = vec![
        vec!['A', 'B', 'C'],
        vec!['Y'],
        vec!['D', 'E'],
        vec!['F', 'G', 'H', 'I', 'J', 'K'],
    ];

    // Print the 2D array
    for row in &char_vec_2d {
        for &char in row {
            print!("{} ", char);
        }
        println!();
    }
}

 
    
/*
run:
 
A B C 
Y 
D E 
F G H I J K 
   
*/

 



answered Feb 8, 2025 by avibootz
...