繁体   English   中英

错误[E0308]:预期类型不匹配 `()`,发现 `bool`。 如何消除此错误?

[英]error[E0308]: mismatched types expected `()`, found `bool`. How to remove this error?

检查回文的程序

pub fn is_palindrome(str: &str, start: i32, end: i32) -> bool

{

    //if empty string
    if str.is_empty() {
        true
    }

    // If there is only one character
    if start == end {
        true
    }

    // If first and last
    // characters do not match
    if str.chars().nth(start as usize) != str.chars().nth(end as usize) {
        false
    }

    // If there are more than
    // two characters, check if
    // middle substring is also
    // palindrome or not.
    if start < end + 1 {
        is_palindrome(str, start + 1, end - 1)
    }
    true
}

这是我在返回语句中收到错误的代码。 它说 error[E0308]: mismatched types expected () , found bool请告诉如何解决这个问题。

如果您想从 function 中更早地返回一个值,您会忘记必须使用的return 语句(另请参见Functions ):

pub fn is_palindrome(str: &str, start: i32, end: i32) -> bool {

    //if empty string
    if str.is_empty() {
        return true;
    }

    // If there is only one character
    if start == end {
        return true;
    }

    // If first and last
    // characters do not match
    if str.chars().nth(start as usize) != str.chars().nth(end as usize) {
        return false;
    }

    // If there are more than
    // two characters, check if
    // middle substring is also
    // palindrome or not.
    if start < end + 1 {
        return is_palindrome(str, start + 1, end - 1);
    }
    true
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM