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.
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.
Define a trait KeyValueStore with:
Key.Value.set to add a key-value pair.get takes a reference of &Key and returns Option<&Value>.Create a struct InMemoryStore that uses a HashMap to store key-value pairs. Implement the KeyValueStore trait 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).
If you're stuck, here are some hints to help you solve the challenge:
Define associated types in the KeyValueStore trait 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>;
}use std::{ collections::HashMap, hash::Hash,};// 1. Finish the trait definitionpub 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K: Eq + Hash, V> { pub storage: HashMap<K, V>,}// 3. Implement the trait for InMemoryStoreimpl<K, V> KeyValueStore for InMemoryStore<K, V> where K: Eq + Hash, { type Key = K; type Value = V; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}// 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);}use std::collections::HashMap;// 1. Finish the trait definitionpub 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>;}// Make sure the fields are publicpub struct InMemoryStore<Key , Value>{ pub storage : HashMap<Key , Value>}// 3. Implement the trait for InMemoryStoreimpl <Key , Value > KeyValueStore for InMemoryStore<Key , Value> where Key : std::hash::Hash + std::cmp::Eq,{ type Key = Key; type Value = Value; fn set(&mut self , key : Self::Key , value : Self::Value){ self.storage.insert(key , value); } fn get(&self , key : &Self::Key) -> Option<&Self::Value>{ self.storage.get(key) }}// 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);}use std::collections::HashMap;// 1. Finish the trait definitionpub 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<Key, Value> { pub storage: HashMap<Key, Value>}impl<Key, Value> KeyValueStore for InMemoryStore<Key, Value> where Key: std::hash::Hash + std::cmp::Eq, { type Key = Key; type Value = Value; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}use std::collections::HashMap;// 1. Finish the trait definitionpub 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<Key, Value> { pub storage: HashMap<Key, Value>}// 3. Implement the trait for InMemoryStoreimpl <Key, Value> KeyValueStore for InMemoryStore<Key, Value> where Key: std::hash::Hash + std::cmp::Eq, { type Key = Key; type Value = Value; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}// 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);}use std::collections::HashMap;// 1. Finish the trait definitionpub 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> ;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<Key,Value> { pub storage: HashMap<Key, Value>}// 3. Implement the trait for InMemoryStoreimpl<Key, Value> KeyValueStore for InMemoryStore<Key, Value>where Key: std::hash::Hash + std::cmp::Eq,{ fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) } fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } type Key = Key; type Value = Value;}// 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);}use std::collections::HashMap;// 1. Finish the trait definitionpub trait KeyValueStore { type Key; type Value; fn get(&self, key: &Self::Key) -> Option<&Self::Value>; fn set(&mut self, key: Self::Key, value: Self::Value);}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<Key, Value> { pub storage: HashMap<Key, Value>,}// 3. Implement the trait for InMemoryStoreimpl<Key, Value> KeyValueStore for InMemoryStore<Key, Value>where Key: std::hash::Hash + std::cmp::Eq,{ type Key = Key; type Value = Value; fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) } fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); }}// 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);}// 1. Finish the trait definitionpub 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicuse std::collections::HashMap;pub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>,}// 3. Implement the trait for InMemoryStoreimpl<K: Eq + std::hash::Hash, V> KeyValueStore for InMemoryStore<K, V> { type Key = K; type Value = V; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}// 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);}use std::{collections::HashMap, hash::Hash};// 1. Finish the trait definitionpub trait KeyValueStore { type Key; type Value; fn get(&self, k: &Self::Key) -> Option<&Self::Value>; fn set(&mut self, k: Self::Key, v: Self::Value);}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>,}impl<K, V> KeyValueStore for InMemoryStore<K, V>where K: Hash + Eq, V: Clone,{ type Key = K; type Value = V; fn get(&self, k: &Self::Key) -> Option<&Self::Value> { self.storage.get(&k) } fn set(&mut self, k: Self::Key, v: Self::Value) { self.storage.insert(k, v); }}// 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);}use std::collections::HashMap;pub trait KeyValueStore { type Key; type Value; fn set (&mut self, key: Self::Key, val: Self::Value); fn get (&self, key: &Self::Key) -> Option<&Self::Value>;}pub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>,}impl<K, V> KeyValueStore for InMemoryStore<K, V>where K: Eq + core::hash::Hash{ type Key = K; type Value = V; fn set (&mut self, key: K, val: V) { self.storage.insert(key, val); } fn get (&self, key: &K) -> Option<&V> { self.storage.get(key) }}pub 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);}use std::collections::HashMap;use std::hash::Hash;// 1. Finish the trait definitionpub trait KeyValueStore { type Key; type Value; fn get(&self, key: &Self::Key) -> Option<&Self::Value>; fn set(&mut self, key: Self::Key, value: Self::Value);}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>,}// 3. Implement the trait for InMemoryStoreimpl<K, V> KeyValueStore for InMemoryStore<K, V> where K: Eq + Hash { type Key = K; type Value = V; fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) } fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); }}// 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);}use std::collections::HashMap;// 1. Finish the trait definitionpub trait KeyValueStore { type Key; type Value; fn get(&self, key: &Self::Key) -> Option<&Self::Value>; fn set(&mut self, key: Self::Key, value: Self::Value);}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>,}// 3. Implement the trait for InMemoryStoreimpl<K: Eq + std::hash::Hash, V> KeyValueStore for InMemoryStore<K, V> { type Key = K; type Value = V; fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) } fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); }}// 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);}use std::collections::HashMap;use std::hash::Hash;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>;}pub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>,}impl<K, V> KeyValueStore for InMemoryStore<K, V>where K: Eq + Hash,{ type Key = K; type Value = V; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}pub 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);}use std::collections::HashMap;use std::hash::Hash;// 1. Finish the trait definitionpub 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>,}// 3. Implement the trait for InMemoryStoreimpl<K, V> KeyValueStore for InMemoryStore<K, V>where K: Eq + Hash{ type Key = K; type Value = V; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}// 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);}use std::collections::HashMap;use std::hash::Hash;// 1. Finish the trait definitionpub 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<Key, Value> { pub storage: HashMap<Key, Value>,}// 3. Implement the trait for InMemoryStoreimpl<Key: Eq + Hash, Value> KeyValueStore for InMemoryStore<Key, Value> { type Key = Key; type Value = Value; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}// 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);}use std::collections::HashMap;use std::hash::Hash;// 1. Finish the trait definitionpub 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>}impl<K,V> KeyValueStore for InMemoryStore<K, V> where K: Eq + Hash{ type Key = K; type Value = V; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}// 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);}use std::{collections::HashMap, hash::Hash};// 1. Finish the trait definitionpub 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>,}// 3. Implement the trait for InMemoryStoreimpl<K, V> KeyValueStore for InMemoryStore<K, V>where K: Eq + Hash,{ type Key = K; type Value = V; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}// 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);}use std::{collections::HashMap, hash::Hash};// 1. Finish the trait definitionpub 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>,}// 3. Implement the trait for InMemoryStoreimpl<K, V> KeyValueStore for InMemoryStore<K, V>where K: Eq + Hash,{ type Key = K; type Value = V; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}// 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);}use std::collections::HashMap;use std::hash::Hash;// 1. Finish the trait definitionpub 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>,}// 3. Implement the trait for InMemoryStoreimpl<K, V> KeyValueStore for InMemoryStore<K, V> where K: Eq + Hash { type Key = K; type Value = V; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}// 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);}use std::collections::HashMap;use std::hash::Hash;// 1. Finish the trait definitionpub 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<Key, Value> { pub storage: HashMap<Key, Value>}// 3. Implement the trait for InMemoryStoreimpl<Key, Value> KeyValueStore for InMemoryStore<Key, Value>where Key: Eq + Hash { type Key = Key; type Value = Value; fn set(&mut self, key: Key, value: Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}// 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);}use std::{collections::HashMap, hash::Hash};// 1. Finish the trait definitionpub 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>,}// 3. Implement the trait for InMemoryStoreimpl<K, V> KeyValueStore for InMemoryStore<K, V>where K: Eq + Hash,{ type Key = K; type Value = V; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}// 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);}use std::collections::HashMap;use std::hash::Hash;// 1. Finish the trait definitionpub 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>}// 3. Implement the trait for InMemoryStoreimpl <K, V> KeyValueStore for InMemoryStore<K, V> where K: Eq + Hash { type Key = K; type Value = V; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}// 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);}use std::collections::HashMap;use std::hash::Hash;// 1. Finish the trait definitionpub 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>}// 3. Implement the trait for InMemoryStoreimpl <K, V> KeyValueStore for InMemoryStore<K, V> where K: Eq + Hash { type Key = K; type Value = V; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}// 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);}use std::collections::HashMap;use std::hash::Hash;// 1. Finish the trait definitionpub 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<T, U> { pub storage: HashMap<T, U>}// 3. Implement the trait for InMemoryStoreimpl<T: Eq + Hash, U> KeyValueStore for InMemoryStore<T, U> { type Key = T; type Value = U; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value>{ self.storage.get(key) }}// 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);}use std::collections::HashMap;// 1. Finish the trait definitionpub 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>}// 3. Implement the trait for InMemoryStoreimpl <K,V> KeyValueStore for InMemoryStore<K, V>where K: Eq + std::hash::Hash,{ type Key = K; type Value = V; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}// 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);}use std::collections::HashMap;// 1. Finish the trait definitionpub 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>}// 3. Implement the trait for InMemoryStoreimpl<K, V> KeyValueStore for InMemoryStore<K, V> where K: Eq + std::hash::Hash,{ type Key = K; type Value = V; fn set(&mut self, key: K, value: V) { self.storage.insert(key, value); } fn get(&self, key: &K) -> Option<&V> { self.storage.get(key) }}// 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);}use std::collections::HashMap;// 1. Finish the trait definitionpub 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>,}impl<K, V> KeyValueStore for InMemoryStore<K, V>where K: Eq + std::hash::Hash,{ type Key = K; type Value = V; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}// 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);}use std::collections::HashMap;// 1. Finish the trait definitionpub trait KeyValueStore { type Key: std::cmp::Eq + std::hash::Hash; type Value; fn set(&mut self, key: Self::Key, value: Self::Value); fn get(&self, key: &Self::Key) -> Option<&Self::Value>;}// Make sure the fields are publicpub struct InMemoryStore<Key, Value>where Key: std::cmp::Eq + std::hash::Hash{ pub storage: HashMap<Key, Value>,}// 3. Implement the trait for InMemoryStoreimpl<K, V> KeyValueStore for InMemoryStore<K, V>where K: std::cmp::Eq + std::hash::Hash,{ type Key = K; type Value = V; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}// 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);}use std::collections::HashMap;use std::hash::Hash;use std::cmp::Eq;// 1. Finish the trait definitionpub trait KeyValueStore<Key, Value> { fn set(&mut self, key: Key, value: Value); fn get(&self, key: &Key) -> Option<&Value>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<Key, Value> { pub storage: HashMap<Key, Value>,}// 3. Implement the trait for InMemoryStoreimpl<Key, Value> KeyValueStore<Key, Value> for InMemoryStore<Key, Value> where Key: Eq + Hash,{ fn set(&mut self, key: Key, value: Value) { self.storage.insert(key, value); } fn get(&self, key: &Key) -> Option<&Value> { self.storage.get(key) }}// 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);}use std::{collections::HashMap, hash::Hash};// 1. Finish the trait definitionpub trait KeyValueStore { type Key; type Value; fn get(&self, key: &Self::Key) -> Option<&Self::Value>; fn set(&mut self, key: Self::Key, value: Self::Value);}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>,}impl<K: Hash + Eq, V> KeyValueStore for InMemoryStore<K, V> { type Key = K; type Value = V; fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) } fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); }}// 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);}use std::collections::HashMap;use std::hash::Hash;// 1. Finish the trait definitionpub trait KeyValueStore { type Key; type Value; fn set(&mut self, k: Self::Key, v: Self::Value); fn get(&self, k: &Self::Key) -> Option<&Self::Value>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<T,U>{ pub storage: HashMap<T, U>}// 3. Implement the trait for InMemoryStoreimpl<T: Eq + Hash, U> KeyValueStore for InMemoryStore<T, U> { type Key = T; type Value = U; fn set(&mut self, k: Self::Key, v: Self::Value) { self.storage.insert(k, v); } fn get(&self, k: &Self::Key) -> Option<&Self::Value>{ self.storage.get(k) }}// 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);}use std::{collections::HashMap, hash::Hash};// 1. Finish the trait definitionpub 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>}impl<K: Hash + Eq, V> KeyValueStore for InMemoryStore<K, V> { type Key = K; type Value = V; fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) } fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); }}// 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);}use std::{collections::HashMap, hash::Hash};/* USING GENERICS */// // 1. Finish the trait definition// pub trait KeyValueStore<K,V> {// fn set(&mut self, key: K, value: V);// fn get(&self, key: &K) -> Option<&V>;// }// // 2. Implement the trait for InMemoryStore// // Make sure the fields are public// pub struct InMemoryStore<K, V> {// pub storage: HashMap<K, V>// }// impl<K: Hash + Eq, V> KeyValueStore<K, V> for InMemoryStore<K, V> {// fn set(&mut self, key: K, value: V){// self.storage.insert(key, value);// }// fn get(&self, key: &K) -> Option<&V> {// self.storage.get(key)// }// }/* USING ASSOCIATED TYPES */// 1. Finish the trait definitionpub 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>}impl<K: Hash + Eq, V> KeyValueStore for InMemoryStore<K, V> { type Key = K; type Value = V; fn set(&mut self, key: Self::Key, value: Self::Value){ self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}// 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);}use std::{collections::HashMap, hash::Hash};// 1. Finish the trait definitionpub 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>}impl<K: Hash + Eq, V> KeyValueStore for InMemoryStore<K, V> { type Key = K; type Value = V; fn set(&mut self, key: Self::Key, value: Self::Value){ self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}// 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);}use std::collections::HashMap;use std::hash::Hash;// 1. Finish the trait definition/**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>;}**/pub trait KeyValueStore<K, V> { fn set(&mut self, key: K, value: V); fn get(&self, key: &K) -> Option<&V>;}// Make sure the fields are publicpub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>,}impl<K: Hash + Eq, V> KeyValueStore<K, V> for InMemoryStore<K, V> { /** type Key = K; type Value = V; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) } **/ fn set(&mut self, key: K, value: V) { self.storage.insert(key, value); } fn get(&self, key: &K) -> Option<&V> { self.storage.get(key) }}// 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);} use std::collections::HashMap; use std::hash::Hash; // Define the KeyValueStore trait with associated types 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>; } // Define the InMemoryStore struct with generic parameters pub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>, } // Implement the KeyValueStore trait for InMemoryStore impl<K, V> KeyValueStore for InMemoryStore<K, V> where K: Eq + Hash, { type Key = K; type Value = V; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) } } // Add a constructor for convenience impl<K, V> InMemoryStore<K, V> where K: Eq + Hash, { pub fn new() -> Self { InMemoryStore { storage: HashMap::new(), } } } pub 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);}use std::collections::HashMap;// 1. Trait definition - make it generic or use associated typespub trait KeyValueStore { type Key; type Value; fn get(&self, key: &Self::Key) -> Option<&Self::Value>; fn set(&mut self, key: Self::Key, value: Self::Value);}// 2. InMemoryStore struct with generic parameterspub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>,}// 3. Implement the trait for InMemoryStore with associated typesimpl<K, V> KeyValueStore for InMemoryStore<K, V> where K: std::cmp::Eq + std::hash::Hash { type Key = K; type Value = V; fn get(&self, key: &K) -> Option<&V> { self.storage.get(key) } fn set(&mut self, key: K, value: V) { self.storage.insert(key, value); }}// 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);}use std::collections::HashMap;use std::hash::Hash;// 1. Finish the trait definitionpub trait KeyValueStore<K, V> { fn set(&mut self, key: K, value: V); fn get(&self, key: &K) -> Option<&V>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>,}// 3. Implement the trait for InMemoryStoreimpl<K, V> KeyValueStore<K, V> for InMemoryStore<K, V>where K: Eq + Hash,{ fn set(&mut self, key: K, value: V) { self.storage.insert(key, value); } fn get(&self, key: &K) -> Option<&V> { self.storage.get(key) }}// 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);}use std::{collections::HashMap, hash::Hash};// 1. Finish the trait definitionpub 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>,}// 3. Implement the trait for InMemoryStoreimpl<K: Hash + Eq, V> KeyValueStore for InMemoryStore<K, V> { type Key = K; type Value = V; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}// 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);}use std::collections::HashMap;// 1. Finish the trait definitionpub 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K, V> { pub storage: std::collections::HashMap<K, V>,}impl<K, V> KeyValueStore for InMemoryStore<K, V>where K: std::hash::Hash + Eq,{ type Key = K; type Value = V; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}// 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);}use std::collections::HashMap;// 1. Finish the trait definitionpub trait KeyValueStore<T,V> { fn get(&self,k: &T) -> Option<&V>; fn set(&mut self,k: T, v: V);}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<T, V> where T: Eq + std::hash::Hash{ pub storage: HashMap<T, V>}// 3. Implement the trait for InMemoryStoreimpl <T,V> KeyValueStore<T,V> for InMemoryStore<T,V> where T: Eq + std::hash::Hash { fn set(&mut self, k: T, v: V) { self.storage.insert(k, v); } fn get(&self, key: &T) -> Option<&V> { self.storage.get(key) }}// 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);}use std::collections::HashMap;// 1. Finish the trait definitionpub trait KeyValueStore<K,V> { fn set(&mut self, key:K, value: V); fn get(&self, key:&K) -> Option<&V>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore <K, V>{ pub storage: HashMap<K, V>,}impl<K,V> InMemoryStore<K, V> { pub fn new() -> Self { InMemoryStore { storage: HashMap::new(), } }}impl <K,V> KeyValueStore<K,V> for InMemoryStore<K,V> where K: Eq + std::hash::Hash, V: Clone { fn set(&mut self, key:K, value:V){ self.storage.insert(key, value); } fn get(&self, key:&K) ->Option<&V> { self.storage.get(key) }}// 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);}use std::collections::HashMap;use std::hash::Hash;// 1. Finish the trait definitionpub 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>,}// 3. Implement the trait for InMemoryStoreimpl<K, V> KeyValueStore for InMemoryStore<K, V>where K: Eq + Hash,{ type Key = K; type Value = V; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}// 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);}use std::{collections::HashMap, hash::Hash};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>;}pub struct InMemoryStore<K: Eq + Hash, V> { pub storage: HashMap<K, V>,}impl<K, V> KeyValueStore for InMemoryStore<K, V>where K: Eq + Hash,{ type Key = K; type Value = V; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}// 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);}use std::collections::HashMap;use core::hash::Hash;// 1. Finish the trait definitionpub 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>;}pub struct InMemoryStore<Key, Value> { pub storage: HashMap<Key, Value>,}impl<K, V> KeyValueStorefor InMemoryStore<K, V>where K: Eq + Hash{ type Key = K; type Value = V; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}// 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);}use std::collections::HashMap;use std::hash::Hash;// 1. Finish the trait definitionpub trait KeyValueStore { type Key; type Value; fn get(&self, key: &Self::Key) -> Option<&Self::Value>; fn set(&mut self, key: Self::Key, value: Self::Value);}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K,V> { pub storage: HashMap<K,V>}// 3. Implement the trait for InMemoryStoreimpl<K: Eq + Hash,V> KeyValueStore for InMemoryStore<K,V> { type Key = K; type Value = V; fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) } fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); }}// 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);}use std::collections::HashMap;// 1. Finish the trait definitionpub 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>}// 3. Implement the trait for InMemoryStoreimpl<K,V> KeyValueStore for InMemoryStore<K, V> where K: std::hash::Hash + std::cmp::Eq { type Key = K; type Value = V; fn get(&self, key:&Self::Key) -> Option<&Self::Value> { self.storage.get(key) } fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); }} // 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);}use std::collections::HashMap;// 1. Finish the trait definitionpub 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<KeyType, ValueType> { pub storage: HashMap<KeyType, ValueType>,}// 3. Implement the trait for InMemoryStoreimpl<KeyType: std::cmp::Eq + std::hash::Hash, ValueType> KeyValueStore for InMemoryStore<KeyType, ValueType> { type Key = KeyType; type Value = ValueType; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) } }// 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);}use std::collections::HashMap;use std::hash::Hash;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>;}pub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>,}impl<K, V> InMemoryStore<K, V> { pub fn new() -> Self { InMemoryStore { storage: HashMap::new(), } }}impl<K, V> KeyValueStore for InMemoryStore<K, V>where K: Eq + Hash,{ type Key = K; type Value = V; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}use std::{collections::HashMap, hash::Hash};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>;}pub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>,}impl<K, V> KeyValueStore for InMemoryStore<K, V>where K: Eq + Hash,{ type Key = K; type Value = V; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}use std::{collections::HashMap, hash::Hash};// 1. Finish the trait definitionpub 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>}// 3. Implement the trait for InMemoryStoreimpl<K: Eq + Hash, V> KeyValueStore for InMemoryStore<K, V> { type Key = K; type Value = V; fn set(&mut self, key: Self::Key, value: Self::Value) { self.storage.insert(key, value); } fn get(&self, key: &Self::Key) -> Option<&Self::Value> { self.storage.get(key) }}// 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);}