Simple String Utilities Library
This challenge asks you to create a simple library crate in Rust that provides a few utility functions for manipulating strings. Building a library crate is a fundamental skill in Rust development, allowing you to encapsulate reusable code and share it across multiple projects. This exercise will reinforce your understanding of Rust's module system, visibility, and crate structure.
Problem Description
You are tasked with creating a new Rust crate named string_utils that provides the following functionalities:
reverse_string(s: &str) -> String: This function takes a string slice (&str) as input and returns a newStringcontaining the reversed version of the input string.count_words(s: &str) -> usize: This function takes a string slice (&str) as input and returns the number of words in the string. Words are separated by whitespace (spaces, tabs, newlines). Leading and trailing whitespace should be ignored.is_palindrome(s: &str) -> bool: This function takes a string slice (&str) as input and returnstrueif the string is a palindrome (reads the same forwards and backward, ignoring case and non-alphanumeric characters), andfalseotherwise.
The crate should be structured with a public module named string_utils containing these functions. All functions should be publicly accessible.
Examples
Example 1:
Input: "hello"
Output: "olleh"
Explanation: The string "hello" reversed is "olleh".
Example 2:
Input: "This is a test"
Output: 4
Explanation: The string "This is a test" contains four words: "This", "is", "a", and "test".
Example 3:
Input: "A man, a plan, a canal: Panama"
Output: true
Explanation: Ignoring case and non-alphanumeric characters, the string becomes "amanaplanacanalpanama", which is a palindrome.
Example 4:
Input: "race a car"
Output: false
Explanation: Ignoring case, the string becomes "raceacar", which is not a palindrome.
Constraints
- The input string
scan be of any length, including empty strings. - The
reverse_stringfunction should allocate a newStringand not modify the original input. - The
count_wordsfunction should handle strings with multiple spaces between words correctly. - The
is_palindromefunction should ignore case and non-alphanumeric characters (letters and numbers only). All other characters should be ignored when determining if a string is a palindrome. - Performance: The functions should be reasonably efficient for typical string lengths (up to a few thousand characters). Avoid excessively complex algorithms unless necessary.
Notes
- You will need to create a new Rust project and structure it as a library crate.
- Use Rust's standard library functions for string manipulation.
- Consider using iterators and functional programming techniques for concise and efficient code.
- Remember to include a
Cargo.tomlfile with the necessary dependencies and crate metadata. - The
is_palindromefunction will require filtering out non-alphanumeric characters and converting the string to lowercase before comparison. You can use thechartype and its methods for this purpose. - Test your functions thoroughly with various inputs, including edge cases like empty strings, strings with only whitespace, and strings with mixed case and special characters.