[英]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.