fn is_palindrome(string: &str) -> bool {
let mut reversed_string = String::new();
for c in string.chars().rev() {
reversed_string.push(c);
}
return string == reversed_string;
}
fn main() {
let string = "racecar";
let is_palindrome = is_palindrome(string);
println!("The string {} is {}a palindrome", string, if is_palindrome { "" } else { "not "));
}
This program first defines a function called is_palindrome that takes a string as input and returns a bool value. The function first creates a new string called
reversed_string. Then, the function iterates through the string
string, adding each character to the reversed_string in reverse order. Finally, the function compares the
string and reversed_string and returns true if they are equal. Otherwise, the function returns false.
The main function of the program then calls the is_palindrome function with the string "racecar" as input. The result of the function call is then printed to the console.
To run the program, you can save it as a file called is_palindrome.rs and then compile it using the following command:
Code snippet
rustc is_palindrome.rs
Once the program is compiled, you can run it using the following command:
Code snippet
./is_palindrome
This will print the following output to the console:
Code snippet
The string racecar is a palindrome
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here is a Rust program to check if a given string is a valid palindrome:
Rust
This program first defines a function called
is_palindromethat takes a string as input and returns a bool value. The function first creates a new string calledreversed_string. Then, the function iterates through the stringstring, adding each character to thereversed_stringin reverse order. Finally, the function compares thestringandreversed_stringand returns true if they are equal. Otherwise, the function returns false.The main function of the program then calls the
is_palindromefunction with the string "racecar" as input. The result of the function call is then printed to the console.To run the program, you can save it as a file called
is_palindrome.rsand then compile it using the following command:Code snippet
Once the program is compiled, you can run it using the following command:
Code snippet
This will print the following output to the console:
Code snippet