Reputation: 123
program to check palindrome
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
}
This is my code I get error in return statements.
It says
error[E0308]: mismatched types
expected ()
, found bool
Please tell how to solve this.
Upvotes: 0
Views: 1613
Reputation: 210909
You forget the 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
}
Upvotes: 2