错误 [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
}
这是我的代码,我在 return 语句中出错。
它说
错误[E0308]:类型不匹配
预期 ()
,发现 bool
请告诉如何解决这个问题。
你忘了return statements which has to be used if you want to return a value earlier from within a function (See also 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
}
检查回文的程序
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
}
这是我的代码,我在 return 语句中出错。
它说
错误[E0308]:类型不匹配
预期 ()
,发现 bool
请告诉如何解决这个问题。
你忘了return statements which has to be used if you want to return a value earlier from within a function (See also 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
}