Hone logo
Hone
Problems

Implementing a Simple Crate (Box) in Rust

This challenge involves creating a fundamental data structure: a crate, which will act as a simple container or box for holding a value of a generic type. This is a foundational concept in Rust for understanding ownership, generics, and basic struct implementation.

Problem Description

Your task is to implement a generic struct named Crate<T> in Rust. This Crate should be capable of holding a single value of any type T. You will need to define the struct, a constructor function to create new Crate instances, and a method to retrieve the value stored within the Crate.

Key Requirements:

  • Generic Type: The Crate struct must be generic over a type parameter T, allowing it to hold values of any data type (e.g., i32, String, custom structs).
  • Storage: The Crate struct should have a single field to store the value of type T.
  • Constructor: Implement a public associated function (like a constructor) named new that takes a value of type T and returns a new Crate<T> instance containing that value.
  • Accessor Method: Implement a public method named get that takes &self and returns a reference to the value stored inside the Crate. This method should allow inspection of the contained value without taking ownership.

Expected Behavior:

  • When a Crate is created with a specific value, it should store that value internally.
  • The get method should return a reference to the stored value, allowing you to read it.

Edge Cases:

  • Consider how your implementation handles different types, including primitive types, heap-allocated types (like String), and potentially types that implement Copy.

Examples

Example 1:

Input:
let mut int_crate = Crate::new(42);
let value = int_crate.get();

Output:
*value == 42

Explanation: A Crate is created to hold an i32. The get method returns a reference to the i32 value 42.

Example 2:

Input:
let mut string_crate = Crate::new(String::from("hello"));
let value_ref = string_crate.get();

Output:
*value_ref == "hello"

Explanation: A Crate is created to hold a String. The get method returns a reference to the String, which can be dereferenced to compare its content.

Example 3:

Input:
#[derive(Debug, PartialEq, Clone)]
struct Point {
    x: i32,
    y: i32,
}
let point_crate = Crate::new(Point { x: 10, y: 20 });
let point_ref = point_crate.get();

Output:
point_ref.x == 10 && point_ref.y == 20

Explanation: A Crate can hold a custom struct. The get method returns a reference to the Point struct.

Constraints

  • The Crate struct must be defined as pub struct Crate<T>.
  • The new function must be a public associated function: pub fn new(value: T) -> Self.
  • The get method must be a public method: pub fn get(&self) -> &T.
  • Your solution should compile successfully with rustc.
  • No external crates are allowed for the core Crate implementation.

Notes

  • This challenge focuses on basic Rust syntax for structs, generics, associated functions, and methods.
  • Think about how references work in Rust when implementing the get method.
  • You do not need to implement Drop or any complex lifetime management for this basic Crate.
Loading editor...
rust