Implementing a Generic Data Container in Rust
This challenge focuses on creating a reusable data structure in Rust using generics. Generics allow you to write code that can work with different data types without needing to be rewritten for each type, promoting code reusability and type safety. Your task is to implement a struct that can hold a single value of any type, demonstrating your understanding of Rust's generic capabilities.
Problem Description
You are required to implement a struct named Container that can hold a single value of any type T. This struct should be generic, meaning the type T is specified when the struct is instantiated. The Container struct should have a single field named value of type T. You must also implement a method called get_value that returns a reference to the stored value.
Key Requirements:
- The
Containerstruct must be generic over a typeT. - The struct must have a private field
valueof typeT. - The struct must have a public method
get_valuethat returns a reference&Tto the stored value. - The code must compile without warnings.
Expected Behavior:
The Container struct should be able to hold any type of data, and the get_value method should return a reference to that data. The reference should be immutable, preventing modification of the stored value through the method.
Edge Cases to Consider:
- The type
Tcan be any valid Rust type, including primitive types (i32, f64, bool, etc.), strings, other structs, and enums. - Consider the lifetime of the stored value. The
Containershould not outlive the value it holds.
Examples
Example 1:
Input:
let mut container: Container<i32> = Container { value: 10 };
let retrieved_value = container.get_value();
Output:
retrieved_value == 10
Explanation: A Container is created holding an integer value of 10. The get_value method returns a reference to this integer, which is then compared to 10, confirming the value was correctly retrieved.
Example 2:
Input:
let mut container: Container<String> = Container { value: "Hello".to_string() };
let retrieved_value = container.get_value();
Output:
retrieved_value == "Hello"
Explanation: A Container is created holding a String value. The get_value method returns a reference to this String, which is then compared to "Hello", confirming the value was correctly retrieved.
Example 3: (Edge Case - Complex Type)
Input:
struct MyStruct {
x: i32,
y: f64,
}
let my_struct = MyStruct { x: 5, y: 2.5 };
let mut container: Container<MyStruct> = Container { value: my_struct };
let retrieved_value = container.get_value();
Output:
retrieved_value.x == 5 && retrieved_value.y == 2.5
Explanation: A Container is created holding a custom struct MyStruct. The get_value method returns a reference to this struct, allowing access to its fields, which are then compared to their expected values.
Constraints
- The solution must be written in Rust.
- The code must compile and run without errors.
- The
get_valuemethod must return a reference&T, not ownership. - The
valuefield within theContainerstruct must be private. - The solution should be concise and readable.
Notes
- Think about how generics work in Rust and how to specify the type parameter for your struct.
- Consider the ownership and borrowing rules when implementing the
get_valuemethod. You want to return a reference, not move the value out of the struct. - Rust's compiler will be your friend here. Pay close attention to any error messages and use them to guide your implementation.