---
title: "Write a Rust program to check if a given string is a valid palindrome."  
description: "Write a Rust program to check if a given string is a valid palindrome."  
author: "Utpal Vishwas"  
published: 2023-06-22  
updated: 2023-06-23  
canonical: https://www.mindstick.com/forum/158833/write-a-rust-program-to-check-if-a-given-string-is-a-valid-palindrome  
category: "rust"  
tags: ["programs", "rust"]  
reading_time: 2 minutes  

---

# Write a Rust program to check if a given string is a valid palindrome.

Write a [Rust](https://www.mindstick.com/forum/160170/explain-rust-s-ownership-borrowing-and-lifetimes-system) [program to check](https://www.mindstick.com/forum/157542/write-a-java-program-to-check-if-a-list-of-integers-contains-only-odd-numbers) if a given [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) is a valid palindrome.

## Replies

### Reply by Aryan Kumar

Sure, here is a [Rust program](https://www.mindstick.com/forum/158800/create-a-rust-program-to-find-the-gcd-greatest-common-divisor-of-two-numbers) to [check if](https://www.mindstick.com/forum/12878/how-to-check-if-an-asp-dot-net-file-upload-control-has-a-file-in-jquery) a given string is a valid palindrome:

Rust

```plaintext
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

```plaintext
rustc is_palindrome.rs
```

Once the program is compiled, you can run it using the following command:

Code snippet

```plaintext
./is_palindrome
```

This will print the following output to the console:

Code snippet

```plaintext
The string racecar is a palindrome
```


---

Original Source: https://www.mindstick.com/forum/158833/write-a-rust-program-to-check-if-a-given-string-is-a-valid-palindrome

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
