Inspirational Quote Generator in Rust
This challenge asks you to build a simple command-line application in Rust that generates inspirational quotes. The program should read quotes from a file, randomly select one, and print it to the console. This is a practical exercise in file I/O, random number generation, and string manipulation, all fundamental aspects of Rust programming.
Problem Description
You need to create a Rust program that reads inspirational quotes from a text file, selects a random quote from the file, and prints it to the standard output. The quotes in the file are assumed to be separated by newline characters (\n). The program should handle the case where the file is empty or doesn't exist gracefully.
Key Requirements:
- File Reading: The program must be able to read the contents of a specified text file.
- Random Selection: It must randomly select one quote from the list of quotes read from the file.
- Output: The selected quote should be printed to the console.
- Error Handling: The program should handle potential errors such as file not found or an empty file. If the file is not found, print an appropriate error message to the console. If the file is empty, print a message indicating that no quotes were found.
- Command-Line Argument: The program should accept the filename as a command-line argument.
Expected Behavior:
- The program should take a single command-line argument: the path to the quotes file.
- If the file exists and contains quotes, the program should print a randomly selected quote to the console.
- If the file does not exist, the program should print an error message to the console: "Error: File not found."
- If the file exists but is empty, the program should print a message to the console: "No quotes found in the file."
Edge Cases to Consider:
- Empty File: The file exists but contains no quotes.
- File Not Found: The specified file does not exist.
- Invalid File Path: The provided file path is invalid (e.g., contains illegal characters).
- Large File: The file contains a very large number of quotes (consider memory usage, though this is less critical for this exercise).
- Quotes with Newlines: Quotes themselves might contain newline characters. The program should treat the entire content between two newline characters as a single quote.
Examples
Example 1:
Input: quotes.txt (containing "Live more with less.\nAs you think, so shall you be.\nDo not dwell in the past, do not dream of the future, concentrate the mind on the present moment.")
Command Line: ./quote_generator quotes.txt
Output: As you think, so shall you be.
Explanation: The program reads the quotes from quotes.txt, randomly selects one, and prints it.
Example 2:
Input: non_existent_file.txt
Command Line: ./quote_generator non_existent_file.txt
Output: Error: File not found.
Explanation: The file non_existent_file.txt does not exist, so the program prints an error message.
Example 3:
Input: empty_quotes.txt (an empty file)
Command Line: ./quote_generator empty_quotes.txt
Output: No quotes found in the file.
Explanation: The file empty_quotes.txt exists but is empty, so the program prints a message indicating that no quotes were found.
Constraints
- File Size: The quotes file is expected to be reasonably sized (less than 1MB).
- Input Format: Quotes are separated by newline characters (
\n). - Error Handling: The program must handle file I/O errors gracefully.
- Command-Line Argument: The program must accept the filename as a command-line argument.
- Rust Version: Use Rust 1.60 or later.
Notes
- Consider using the
std::fsmodule for file I/O. - The
randcrate can be used for random number generation. Add it to yourCargo.tomlfile. - The
std::envmodule can be used to access command-line arguments. - Think about how to split the file content into individual quotes. String splitting based on newline characters is a good starting point.
- Error handling is crucial. Use
Resultto manage potential errors during file operations. - Focus on clarity and readability of your code. Good variable names and comments are appreciated.