79415991

Date: 2025-02-05 19:35:23
Score: 3.5
Natty:
Report link

How about this?

fn flatten_vectors(vectors: Vec<Vec<i32>>) -> Vec<i32> {
    let mut result = Vec::new();
    let max_len = vectors.iter().map(|v| v.len()).max().unwrap_or(0);

    for i in 0..max_len {
        for vec in &vectors {
            if let Some(&val) = vec.get(i) {
                result.push(val);
            }
        }
    }

    result
}

fn main() {
    let v = vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]];
    let flattened = flatten_vectors(v);
    println!("{:?}", flattened);
}


Output: [1, 4, 7, 2, 5, 8, 3, 6, 9]

Playground

Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Starts with a question (0.5): How
  • Low reputation (1):
Posted by: Ron Slosberg