Implementing Auto Traits with impl Trait in Rust
Rust's impl Trait feature, often referred to as "auto traits," allows you to return types from functions without explicitly naming the concrete type that implements a trait. This enhances code flexibility and abstraction, particularly when dealing with complex trait implementations. This challenge will guide you through implementing a scenario where impl Trait significantly simplifies your code.
Problem Description
You are tasked with creating a function get_data() that returns a value implementing the Display trait. The specific type implementing Display should be determined at compile time based on the input parameter. The function should accept an integer size as input, and return a Vec<String> of the specified size, where each string in the vector contains its index as a string. You must use impl Trait to return the Vec<String> without explicitly specifying the type.
Key Requirements:
- The
get_data()function must accept an integersizeas input. - The function must return a
Vec<String>of the specifiedsize. - Each element in the
Vec<String>must be a string representation of its index (starting from 0). - The returned
Vec<String>must implement theDisplaytrait. - You must use
impl Traitin the function signature to avoid explicitly naming the return type.
Expected Behavior:
When get_data(5) is called, it should return a Vec<String> containing ["0", "1", "2", "3", "4"]. The type of the returned value should be inferred by the compiler and not explicitly stated.
Edge Cases to Consider:
sizeis 0: Should return an emptyVec<String>.sizeis negative: The function should panic with a descriptive error message.
Examples
Example 1:
Input: 5
Output: ["0", "1", "2", "3", "4"]
Explanation: The function creates a vector of size 5, where each element is the string representation of its index.
Example 2:
Input: 0
Output: []
Explanation: The function returns an empty vector when the size is 0.
Example 3:
Input: -1
Output: (Panics with "Size must be non-negative")
Explanation: The function panics when the size is negative.
Constraints
- The
sizeinput will be ani32. - The
Displaytrait is part of the standard library (std::fmt::Display). - The function must not use any external crates.
- The function should be efficient; creating the vector directly is preferred over appending elements one by one.
Notes
impl Traitallows the compiler to infer the return type based on the function body.- Consider using a loop or a map operation to efficiently create the vector of strings.
- Remember to handle the edge case where
sizeis negative. Usepanic!for error handling. - The
Displaytrait is used for converting a type to a string representation. You don't need to implement it yourself; theStringtype already implements it.
use std::fmt::Display;
fn get_data(size: i32) -> impl Trait {
if size < 0 {
panic!("Size must be non-negative");
}
let mut data: Vec<String> = Vec::with_capacity(size as usize);
for i in 0..size {
data.push(i.to_string());
}
data
}