I am new to rust programming. I wanted to implement merge sort with recursion. Here is my code:
fn merge(a: &mut Vec
In Rust, the block type is the type of the final expression or ()
if there is none. Also combined blocks needs to be same type like if{...} else if{...} else{...}
. Without an else
the return type of an if expression must be ()
as this is the type that is returned when the expression evaluates to false.
Additionally the result is not the final expression in your code. What you need instead is to use return
. Also be aware Vec::push
requires a mutable reference of instance(&mut self
).
if n == 1 {
println!("The divided vector is: {:?}", v.to_vec());
let mut t: Vec<u32> = Vec::new();
t.push(v[0]);
return t;
}