Associated types in Rust allow you to define traits with a type placeholder that implementors of the trait can specify. This enables more flexible and type-safe abstractions.
For example, instead of passing types as generic parameters every time, you can use associated types to simplify the interface. Associated types are particularly useful when working with collections, iterators, or data processing pipelines.
In this challenge, you'll define a trait with an associated type and implement it for a struct. The goal is to create a structure for managing a simple Key-Value store where keys and values can have different types specified by the trait implementation.
You can solve the problem using either generics or associated types, but the challenge is made to make you familiar with associated types, so try to solve the challenge by using them instead of generics.
Your Task
Define a trait KeyValueStore with an associated type Key and Value. Implement this trait for the struct InMemoryStore. The implementation should allow storing and retrieving key-value pairs of the specified types.
Requirements
-
Define a trait
KeyValueStorewith:- An associated type
Key. - An associated type
Value. - Methods:
setto add a key-value pair.gettakes a reference of&Keyand returnsOption<&Value>.
- An associated type
-
Create a struct
InMemoryStorethat uses aHashMapto store key-value pairs. Implement theKeyValueStoretrait for this struct. -
Make sure all relevant values are public so that they can be accessed from outside the module (essential to pass the tests).
Hints
If you're stuck, here are some hints to help you solve the challenge:
<details> <summary>Click here to reveal hints</summary>-
Define associated types in the
KeyValueStoretrait like this:pub trait KeyValueStore {type Key;type Value;fn set(&mut self, key: Self::Key, value: Self::Value);fn get(&self, key: &Self::Key) -> Option<&Self::Value>;}
// 1. Finish the trait definitionpub trait KeyValueStore// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore// 3. Implement the trait for InMemoryStore// Example usagepub fn main() {let mut store: InMemoryStore<String, String> = InMemoryStore {storage: HashMap::new(),};store.set("name".to_string(), "Rust".to_string());assert_eq!(store.get(&"name".to_string()), Some(&"Rust".to_string()));store.set("language".to_string(), "Rust".to_string());assert_eq!(store.get(&"language".to_string()),Some(&"Rust".to_string()));assert_eq!(store.get(&"non_existent".to_string()), None);}