I have a programming assignment of creating a bubble sort using Rust. I don't really have much experience in Rust so this is a little bit hard for me:
fn main() {
println!("Sort numbers ascending");
let num:[i32; 10] = [4, 65, 2, -31, 0, 99, 2, 83, 782, 1];
println!("Before: {:?}", num);
bubble_sort( num);
println!("After: {:?}\n", num);
}
fn swap( a: &mut i32, b: &mut i32){
let tmp: i32 = *b;
*b=*a;
*a=tmp;
}
fn bubble_sort(mut arr: [i32; 10]) {
for i in 0..arr.len() {
for j in 0..arr.len() - 1 - i {
if arr[j] > arr[j + 1] {
swap( &mut arr[j], &mut arr[j + 1]);
}
}
}
}
Error:
Compiling playground v0.0.1 (/playground)
error[E0499]: cannot borrow `arr[_]` as mutable more than once at a time
--> src/main.rs:19:24
|
19 | swap( &mut arr[j], &mut arr[j + 1]);
| ---- ----------- ^^^^^^^^^^^^^^^ second mutable borrow occurs here
| | |
| | first mutable borrow occurs here
| first borrow later used by call
|
= help: consider using `.split_at_mut(position)` or similar method to obtain two mutable non-overlapping sub-slices
For more information about this error, try `rustc --explain E0499`.
error: could not compile `playground` due to previous error