错误的原因(),在 Rust 中找到了 struct `std::vec::Vec`? [英] Reason of the error expected (), found struct `std::vec::Vec` in Rust?

查看:69
本文介绍了错误的原因(),在 Rust 中找到了 struct `std::vec::Vec`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Rust 编程的新手.我想用递归实现归并排序.这是我的代码:

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

fn merge(a: &mut Vec<u32>, b: &mut Vec<u32>) -> Vec<u32> {
    let mut temp: Vec<u32> = Vec::new();

    println!("The digit is {}", a[0]);
    while a.len() > 0 && b.len() > 0 {
        if a[0] > b[0] {
            temp.push(a[0]);
            a.pop();
        } else {
            temp.push(b[0]);
            b.pop();
        }
    }

    while a.len() > 0 {
        temp.push(a[0]);
        a.pop();
    }

    while b.len() > 0 {
        temp.push(b[0]);
        b.pop();
    }

    temp
}

fn merge_sort(v: &mut Vec<u32>) -> Vec<u32> {
    println!("The divided vector is: {:?}", v);
    let n = v.len();

    if n == 1 {
        println!("The divided vector is: {:?}", v.to_vec());
        let t: Vec<u32> = Vec::new();
        t.push(v[0]);
        t
    }

    if n == 0 {
        panic!("Alas!! NULL");
    }

    merge(
        &mut merge_sort(&mut v[0..n / 2].to_vec()),
        &mut merge_sort(&mut v[n / 2 + 1..n].to_vec()),
    )
    .to_vec()
}

fn main() {
    let mut v = vec![23, 78, 89, 64, 23, 12, 79, 45, 64];
    println!("The vector is: {:?}", v);
    println!("The length {}", v.len());

    let v = merge_sort(&mut v);

    println!("The sorted vector is: {:?}", v);
}

问题是,当我尝试编译它时,出现以下错误:

The problem is, when I am trying to compile it, I am getting the following error:

error[E0308]: mismatched types
  --> src/main.rs:36:9
   |
32 | /     if n == 1 {
33 | |         println!("The divided vector is: {:?}", v.to_vec());
34 | |         let t: Vec<u32> = Vec::new();
35 | |         t.push(v[0]);
36 | |         t
   | |         ^ expected `()`, found struct `std::vec::Vec`
37 | |     }
   | |     -- help: consider using a semicolon here
   | |_____|
   |       expected this to be `()`
   |
   = note: expected unit type `()`
                 found struct `std::vec::Vec<u32>`

你知道为什么我会收到这个奇怪的错误!看来,我错过了什么.

Do you have any idea why am I getting this strange error! It seems, I am missing something.

推荐答案

在 Rust 中,块类型是最终表达式的类型,或者 () 如果没有.此外,组合 块需要与 if{...} else if{...} else{...} 相同类型.如果没有 else,if 表达式的返回类型必须是 (),因为这是表达式计算结果为 false 时返回的类型.

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.

此外,结果不是代码中的最终表达式.你需要的是使用 return.还要注意 Vec::push 需要实例的可变引用(&mut self).

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;
}

这篇关于错误的原因(),在 Rust 中找到了 struct `std::vec::Vec`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆