了解Rust的函数返回类型

问题描述 投票:1回答:2

我正在尝试从Rust的书中学习它。最常见的事情是有趣的添加。我尝试编写一些代码以适应它,所以我决定按照本章的说明进行操作,并编写一个快速的斐波那契函数,但是出现错误,我无法解决问题。任何防锈专家都会向我解释为什么会发生这种情况及其背后的逻辑。

fn fibo(x: i32) -> i32 {
    if x == 0 {
        0
    }
    else if x == 1 {
        1
    }

    fibo(x-1) + fibo(x-2)    
}

[当我尝试构建此代码时,出现以下错误;

error[E0308]: mismatched types
 --> src/main.rs:6:9
  |
6 |         0
  |         ^ expected (), found integer
  |
  = note: expected type `()`
             found type `{integer}`

error[E0308]: mismatched types
 --> src/main.rs:9:9
  |
9 |         1
  |         ^ expected (), found integer
  |
  = note: expected type `()`
             found type `{integer}`

但是如果我将代码更改为以下代码,则可以正常工作;

fn fibo(x: i32) -> i32 {
    if x == 0 {
        0
    }
    else if x == 1 {
        1
    }else{
        fibo(x-1) + fibo(x-2)    
    }
}

在Rust语言书中,声明编译器检查if-else块中所有表达式的类型,但也声明如果不存在else语句,它将传递到下一行代码。既然我说过返回类型将是i32,那么编译器怎么会期望类型“()”,如错误所示?

function rust return-type
2个回答
0
投票

这里的问题是,您尝试使用两个“ return”语句。

当一个块的最后一个语句缺少分号时,该语句的结果就是评估整个块的类型。在伪锈中,我们可以说以下

{
    0
} -> usize

就是说

let x = { 0 };

0设置新作用域,因为该块中没有分号,所以隐式返回它,然后成为该块的类型。因此,x: usize

所以,这是怎么回事?您的代码具有two隐式返回:

fn fibo(x: i32) -> i32 {
    if x == 0 { // This starts a block
        0 // First implicit return
    }
    else if x == 1 { // This also starts a block
        1 // First implicit return
    } // Therefore this entire if/else statement is an implicit return unless its output
      // is suppressed with a semicolon.

    fibo(x-1) + fibo(x-2) // Here we try to return again, which confuses rust!
} 

因为您的if/else包含隐式返回并且没有被分配给变量,这就像我说以下:

fn fibo(x: i32) -> i32 {
    //For the sake of explanation, let's assume x == 0
    0 // instead of your if/else

    fibo(x-1) + fibo(x-2)
}

哦,不!如何解决这个问题:

fn fibo(x: i32) -> i32 {
    if x == 0 {
        // return 0; // This is an early return; this will break until it reaches the function
        0 // Since this is about implicit returns I'll use them here
    } else if x == 1 {
        1
    } else {
        fibo(x - 1) + fibo(x - 2)
    }
}

0
投票

您正在尝试从以()作为返回值的if-else块中返回。您可以显式指定退货,即:

fn fibo(x: i32) -> i32 {
    if x == 0 {
        return 0;
    }
    else if x == 1 {
        return 1;
    }

    fibo(x-1) + fibo(x-2)    
}

Edit:为了提高清晰度,您所做的是尝试从无法获取任何值的if-else块返回,因此,如果您必须返回该函数,则可以必须明确声明return 0;或其他内容。编辑:在这里,您的代码不起作用,因为if-else块不是函数中的最后内容。例如,这将完美地工作:

fn some_func(val: u8) -> u8 {
    if val == 100 {
       0
    }
    else {
        1
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.