Interleave two sorted arrays To learn Rust, I tried to write a function that takes two sorted arrays of integers and interleaves them into one longer sorted array.

Is this approach ok? Have I made any mistakes?

fn main() {
    let a = [1, 3, 4, 4];
    let b = [0, 2, 5, 6];
    println!("{:?}", merge(&amp;a, &amp;b));
}

fn merge(list_a : &amp;[i32; 4], list_b : &amp;[i32; 4]) -&gt; [i32; 8] {
    let mut merged_list: [i32; 8] = [0; 8];

    let mut idx = 0;
    let mut idx_b = 0;
    for a in list_a.iter() {
        while idx_b &lt; list_b.len() &amp;&amp; list_b[idx_b] &lt; *a {
            merged_list[idx] = list_b[idx_b];
            idx_b +=1;
            idx += 1;
        }
        merged_list[idx] = *a;
        idx += 1;
    }

    for b in list_b[idx_b..].iter() {
        merged_list[idx] = *b;
        idx += 1;
    }
    println!("{:?}", merged_list);
    merged_list
}


#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_merge() {
        let a = [1, 3, 3, 7];
        let b = [0, 4, 6, 8];
        assert_eq!(merge(&amp;a, &amp;b), [0, 1, 3, 3, 4, 6, 7, 8]);
    }
}
I can't see any obvious glaring bugs, but I can make a few suggestions to improve your initial baby steps, namely

Varying length arrays

Take your learning a step further and make the functions take arrays of varying lengths and see if your implementation still works. From the looks of it, it should still work without being heavily modified.

Generic array

I am not too familiar with Rust, but if it supports generics, then why don't you make the function generic so that it can take any two arrays of the same data type and merge them.

Conclusion

That is all. The code didn't really have a lot of ways things can go wrong because of the fixed size of the arrays. But you did a good job with the merge function which is what I think you were going for in the first place. See if you can implement the two suggestions above, then create another post