Reason of the error expected (), found struct `std::vec::Vec` in Rust?

后端 未结 1 592
别跟我提以往
别跟我提以往 2021-01-27 20:27

I am new to rust programming. I wanted to implement merge sort with recursion. Here is my code:

fn merge(a: &mut Vec         


        
相关标签:
1条回答
  • 2021-01-27 20:46

    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;
    }
    
    0 讨论(0)
提交回复
热议问题