Pretend you are a computer science professor. I would like you to explain to me how exactly merge sort algorithm works and what are its benefits compared to bubble sort algorithm. Give an implementation example written in rust programming language
Merge sort is a divide and conquer algorithm that works by recursively splitting the input array in half until the arrays are split into single elements. Then, the sorted arrays are merged back together in sorted order.

The key benefits of merge sort over a simpler algorithm like bubble sort are:

1. It is a stable sort that does not change the relative order of elements with equal keys.
2. It has an efficiency of O(n log n) time complexity, making it a relatively efficient algorithm even for large input sizes. Bubble sort, on the other hand, has a quadratic time complexity of O(n^2), making it very inefficient for larger inputs.
3. It is a generic algorithm that can sort any type of data for which a comparison function exists.

Here is a Rust implementation example:
```rust
fn merge_sort(vec: &mut [i32]) {
    if vec.len() < 2 {
        return;
    }

    let mid = vec.len() / 2;
    let (left, right) = vec.split_at_mut(mid);

    merge_sort(left);
    merge_sort(right);

    merge(left, right, vec);
}

fn merge(left: &mut [i32], right: &mut [i32], vec: &mut [i32]) {
    let mut i = 0;
    let mut j = 0;
    let mut k = 0;

    while i < left.len() && j < right.len() {
        if left[i] < right[j] {
            vec[k] = left[i];
            i += 1;
        } else {
            vec[k] = right[j];
            j += 1;
        }
        k += 1;
    }

    while i < left.len() {
        vec[k] = left[i];
        i += 1;
        k += 1;
    }

    while j < right.len() {
        vec[k] = right[j];
        j += 1;
        k += 1;
    }
}
```
Below is an instruction that describes a task. Write a response that appropriately completes the request.

### Instruction:

Pretend you are a computer science professor. I would like you to explain to me how exactly merge sort algorithm works and what are its benefits compared to bubble sort algorithm. Give an implementation example written in rust programming language

### Response:

Merge sort is a divide and conquer algorithm that works by recursively splitting the input array in half until the arrays are split into single elements. Then, the sorted arrays are merged back together in sorted order.

The key benefits of merge sort over a simpler algorithm like bubble sort are:

1. It is a stable sort that does not change the relative order of elements with equal keys.
2. It has an efficiency of O(n log n) time complexity, making it a relatively efficient algorithm even for large input sizes. Bubble sort, on the other hand, has a quadratic time complexity of O(n^2), making it very inefficient for larger inputs.
3. It is a generic algorithm that can sort any type of data for which a comparison function exists.

Here is a Rust implementation example:
```rust
fn merge_sort(vec: &mut [i32]) {
    if vec.len() < 2 {
        return;
    }

    let mid = vec.len() / 2;
    let (left, right) = vec.split_at_mut(mid);

    merge_sort(left);
    merge_sort(right);

    merge(left, right, vec);
}

fn merge(left: &mut [i32], right: &mut [i32], vec: &mut [i32]) {
    let mut i = 0;
    let mut j = 0;
    let mut k = 0;

    while i < left.len() && j < right.len() {
        if left[i] < right[j] {
            vec[k] = left[i];
            i += 1;
        } else {
            vec[k] = right[j];
            j += 1;
        }
        k += 1;
    }

    while i < left.len() {
        vec[k] = left[i];
        i += 1;
        k += 1;
    }

    while j < right.len() {
        vec[k] = right[j];
        j += 1;
        k += 1;
    }
}
```
