Generic Wrapper Struct
Write a Rust function that defines a generic struct Wrapper
Generic structs are defined with angle brackets after the struct name: Wrapper
Rust's generics are similar to templates in C++ but with important differences: they are type-checked at the definition site, support trait bounds for constraining type parameters, and integrate seamlessly with Rust's trait system. Generic structs are the foundation for many standard library types like Option
Time complexity is O(1) as the method simply moves the value out of the struct. Space complexity is O(1). The generic wrapper adds no overhead beyond the size of T itself.
Edge cases include T being a non-Copy type (the move still works), T being a reference type, and ensuring the method consumes self to move ownership of the inner value to the caller.
Example Input & Output
Negative value
Zero
Wrapper of i32 returns the value
Different i32 value
Another value
Algorithm Flow

Solution Approach
Best Answers
struct Wrapper<T> {
value: T,
}
impl<T> Wrapper<T> {
fn value(self) -> T {
self.value
}
}
fn solution(x: i32) -> i32 {
let w = Wrapper { value: x };
w.value()
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
