Mastering Match Arms in Rust: A Data Processing Challenge
Rust's match expression is a powerful tool for pattern matching and control flow. This challenge will test your understanding of match arms by requiring you to process a stream of data represented by an enum and perform different actions based on the data received. This is a common pattern in data processing, parsing, and state machines.
Problem Description
You are tasked with creating a function process_data that takes a vector of DataPoint enums as input and returns a Result<String, String>. The DataPoint enum represents different types of data points, each with its own associated value. The process_data function should iterate through the input vector and perform the following actions based on the DataPoint variant:
Number(i32): Append the number to a running sum. If the sum exceeds 100, return an error message "Sum exceeded limit!".Text(String): Append the text to a result string, separated by a space.Boolean(bool): If the boolean istrue, append "True" to the result string, separated by a space. If it'sfalse, append "False" to the result string, separated by a space.Unknown(String): Return an error message "Unknown data type: ". followed by the unknown data.
The function should return Ok(result_string) if all data points are processed successfully, where result_string is the concatenated text and boolean values. If an error occurs (sum exceeds limit or an unknown data type is encountered), the function should return Err(error_message).
Examples
Example 1:
Input: vec![DataPoint::Number(10), DataPoint::Text("Hello".to_string()), DataPoint::Boolean(true), DataPoint::Number(20)]
Output: Ok("Hello True".to_string())
Explanation: The numbers 10 and 20 are added (sum = 30, which is within the limit). "Hello" and "True" are appended to the result string.
Example 2:
Input: vec![DataPoint::Number(50), DataPoint::Number(60), DataPoint::Text("World".to_string())]
Output: Err("Sum exceeded limit!".to_string())
Explanation: The numbers 50 and 60 are added (sum = 110, which exceeds the limit). The function immediately returns an error.
Example 3:
Input: vec![DataPoint::Number(10), DataPoint::Unknown("Invalid".to_string())]
Output: Err("Unknown data type: Invalid".to_string())
Explanation: The function encounters an `Unknown` variant and returns an error message.
Constraints
- The input vector
data_pointscan contain up to 100DataPointelements. - The
Stringvalues withinTextandUnknownvariants can have a maximum length of 50 characters. - The
i32values withinNumbervariants will be between -100 and 100 (inclusive). - The function must return a
Result<String, String>. - The result string should not contain trailing spaces.
Notes
- Consider using a mutable variable to accumulate the sum and the result string.
- The
matchexpression is the core of this problem. Think carefully about how to pattern match on theDataPointenum variants. - Remember to handle the error cases appropriately and return the correct error message.
- The order of operations is important. The sum check should happen before appending to the result string.
enum DataPoint {
Number(i32),
Text(String),
Boolean(bool),
Unknown(String),
}
fn process_data(data_points: Vec<DataPoint>) -> Result<String, String> {
let mut sum = 0;
let mut result = String::new();
for data_point in data_points {
match data_point {
DataPoint::Number(num) => {
sum += num;
if sum > 100 {
return Err("Sum exceeded limit!".to_string());
}
}
DataPoint::Text(text) => {
if !result.is_empty() {
result.push(' ');
}
result.push_str(&text);
}
DataPoint::Boolean(b) => {
if !result.is_empty() {
result.push(' ');
}
result.push_str(if b { "True" } else { "False" });
}
DataPoint::Unknown(unknown) => {
return Err(format!("Unknown data type: {}", unknown));
}
}
}
Ok(result)
}