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, 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);}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, 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>where T: Eq, T: Hash,{ pub storage: HashMap<T, U>,}// 3. Implement the trait for InMemoryStoreimpl<T, U> KeyValueStore for InMemoryStore<T, U>where T: Eq, T: Hash,{ 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;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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K: Eq + Hash, V> { pub storage: HashMap<K, V>,}impl<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) }}// 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: Eq + Hash; 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: 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); }}// 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: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);}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<Key, Value>where Key: Hash + Eq,{ pub storage: HashMap<Key, Value>,}impl<Key, Value> KeyValueStore for InMemoryStore<Key, Value>where Key: Hash + 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) }}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, 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: 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) }}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);}// 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<KT, VT>{ pub storage: HashMap<KT, VT>}// 3. Implement the trait for InMemoryStoreimpl<A: std::hash::Hash+Eq, B> KeyValueStore for InMemoryStore<A, B> { type Key = A ; type Value = B ; 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 <A,B> { pub storage: HashMap<A,B>,}// 3. Implement the trait for InMemoryStoreimpl <A: Eq + Hash,B> KeyValueStore for InMemoryStore <A, B> { type Key=A; type Value=B; 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);}// 1. Finish the trait definitionuse 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>;}// 2. Implement the trait for InMemoryStore// Make sure the fields are public#[derive(PartialEq)]pub struct InMemoryStore<Key_: Eq + Hash, Value_: PartialEq>{ pub storage: HashMap<Key_,Value_>}// 3. Implement the trait for InMemoryStoreimpl <Key_:Eq + Hash,Value_: std::cmp::PartialEq >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<&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<Key, Value> { fn get(&self, key: &Key) -> Option<&Value>; fn set(&mut self, key: Key, value: Value);}// 2. Implement the trait for InMemoryStore// Make sure the fields are public#[derive(PartialEq)]pub struct InMemoryStore<Key: Eq + Hash, Value: PartialEq> { pub storage: HashMap<Key, Value>,}// 3. Implement the trait for InMemoryStoreimpl<Key: Eq + Hash, Value: PartialEq> KeyValueStore<Key, Value> for InMemoryStore<Key, Value> { fn get(&self, key: &Key) -> Option<&Value> { self.storage.get(key) } fn set(&mut self, key: Key, value: 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 definitionuse 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>;}// 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: 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);}// 1. Finish the trait definitionuse 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>;}// 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: 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>,}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;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: Eq + Hash,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);}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: Eq + Hash,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);}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<T, U> { pub storage: HashMap<T, U>,}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, 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;// 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 public#[derive(Debug)]pub struct InMemoryStore<K, U> { pub storage: HashMap<K, U>}impl<K: Eq + std::hash::Hash, U> KeyValueStore for InMemoryStore<K, U> { type Key = K; 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) }}// 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; 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 public#[derive(Debug)]pub struct InMemoryStore<K, U> { pub storage: HashMap<K, U>,}impl<K: Eq + std::hash::Hash, U> KeyValueStore for InMemoryStore<K, U> { type Key = K; 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) }}// 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, 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> where K: std::cmp::Eq + std::hash::Hash { pub storage: HashMap<K, V>}// 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;// 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>where K: std::cmp::Eq+ std::hash::Hash{ pub storage: HashMap<K, V>,}// 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;// 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> 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;pub trait KeyValueStore { type Key; type Value; fn get(&self, key: &Self::Key) -> Option<&Self::Value>; fn set(&mut self, key: Self::Key, val: Self::Value);}pub struct InMemoryStore<T, U> { // storage: HashMap<<InMemoryStore as KeyValueStore>::Key, <InMemoryStore as KeyValueStore>::Value> pub storage: HashMap<T, U>,}impl<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: &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;// 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) }}// 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>;}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: 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> where K: Eq + Hash, { 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: 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;use std::hash::Hash;use std::cmp::Eq;// 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 set(& mut self , key:Self::Key , value:Self::Value){ self.storage.insert(key,value); } fn get(& self , key:&Self::Key)->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>where K: Eq + Hash,{ 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::cmp::Eq;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>;}pub struct InMemoryStore<T, U>where T: Eq + Hash,{ pub storage: HashMap<T, U>,}// 3. Implement the trait for InMemoryStoreimpl<T, U> KeyValueStore for InMemoryStore<T, U>where T: Eq + Hash,{ 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);}// 1. Finish the trait definitionuse 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>;}// 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: 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> where K: Eq + Hash,{ pub storage: HashMap<K, V>,}// 3. Implement the trait for InMemoryStoreimpl<K,V> KeyValueStore for InMemoryStore<K,V> where K: Eq + Hash, // NOTE only K needs these traits for HashMap, not V{ 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;use std::hash::Hash;// 1. Finish the trait definitionpub 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>;}// 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, val: Self::Value) { self.storage.insert(key, val); } 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);}// 1. Finish the trait definitionuse 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>;}// 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<Key, Value> { pub storage: HashMap<Key, Value>,}// 3. Implement the trait for InMemoryStoreimpl<Key: Hash + Eq, 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::hash::Hash;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 public\pub struct InMemoryStore<K: Hash + Eq, 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::hash::Hash;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 public\pub struct InMemoryStore<K: Hash + Eq, 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;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: Eq + Hash, I> { pub storage: HashMap<T, I>,}// 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);}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: Eq + Hash, V> { pub storage: HashMap<K, V>,}impl<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) }}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<KeyType: Eq + std::hash::Hash, ValueType> KeyValueStore for InMemoryStore<KeyType, ValueType> { type Key = KeyType; type Value = ValueType; fn set(&mut self, key: KeyType, value: ValueType){ self.storage.insert(key, value); } fn get(&self, key: &KeyType) -> Option<&ValueType> { 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>;}use std::hash::Hash;// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<Key, Value>where Key: Eq + Hash { pub storage: HashMap<Key, Value>,}// 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: 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 public//#[derive(Hash, Eq, PartialEq, Debug)]pub struct InMemoryStore <T1, T2> { pub storage: HashMap<T1, T2>,}// 3. Implement the trait for InMemoryStoreimpl <T1,T2>KeyValueStore for InMemoryStore<T1, T2> where T1:Hash+Eq{ type Key = T1; type Value = T2; 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;pub 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<K,V> where K: Eq + Hash{ 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, k: K, v: V) { self.storage.insert(k, v); } fn get(&self, k: &K) -> Option<&V> { 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<K, V> { fn get(&self, x: &K) -> Option<&V>; fn set(&mut self, x: K, y: V) -> ();}// 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<K, V> for InMemoryStore<K, V> { fn get(&self, x: &K) -> Option<&V> { self.storage.get(x) } fn set(&mut self, x: K, y: V) -> () { self.storage.insert(x, y); }}// 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<K, V> { fn get(&self, x: &K) -> Option<&V>; fn set(&mut self, x: K, y: V) -> ();}// 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<K, V> for InMemoryStore<K, V> { fn get(&self, x: &K) -> Option<&V> { self.storage.get(x) } fn set(&mut self, x: K, y: V) -> () { self.storage.insert(x, y); }}// 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;use std::cmp::Eq;// 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: 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);}// 1. Finish the trait definitionuse std:: {collections::HashMap, hash::Hash, cmp::Eq};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>;}// 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, k: Self::Key, v: Self::Value) { self.storage.insert(k, v); } fn get(&self, k: &Self::Key) -> Option<&v> { 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;// 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<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, 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);}