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 intead 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>;
}
// 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);}use std::collections::HashMap;use std::hash::Hash;// 2. Implement the trait for InMemoryStore// Make sure the fields are publicpub struct InMemoryStore<K, T> { pub storage: HashMap<K, T>}// 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<&V> { 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 definition Define a trait KeyValueStore with an associated type Key and Value. Implement this trait for the struct InMemoryStore. The implementation should allow storing and retrieving key-value pairs of the specified types.// Requirements// Define a trait KeyValueStore with:// An associated type Key.// An associated type Value.// Methods:// 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)use std::collections::HashMap;// Define the KeyValueStore traitpub trait KeyValueStore { type Key; type Value; // Method to set a key-value pair fn set(&mut self, key: Self::Key, value: Self::Value); // Method to get a value by key fn get(&self, key: &Self::Key) -> Option<&Self::Value>;}// Define the InMemoryStore structpub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>,}// Implement the KeyValueStore trait for InMemoryStoreimpl<K, V> KeyValueStore for InMemoryStore<K, V>where K: std::cmp::Eq + std::hash::Hash, // Required for HashMap keys{ 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, 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: Hash + 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;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: 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<T, V> { pub storage: HashMap<T, V>,}// 3. Implement the trait for InMemoryStoreimpl<T, V> KeyValueStore for InMemoryStore<T, V>where T: std::cmp::Eq + std::hash::Hash,{ type Key = T; 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 definition Define a trait KeyValueStore with an associated type Key and Value. Implement this trait for the struct InMemoryStore. The implementation should allow storing and retrieving key-value pairs of the specified types.// Requirements// Define a trait KeyValueStore with:// An associated type Key.// An associated type Value.// Methods:// 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)use std::collections::HashMap;// Define the KeyValueStore traitpub trait KeyValueStore { type Key; type Value; // Method to set a key-value pair fn set(&mut self, key: Self::Key, value: Self::Value); // Method to get a value by key fn get(&self, key: &Self::Key) -> Option<&Self::Value>;}// Define the InMemoryStore structpub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>,}// Implement the KeyValueStore trait for InMemoryStoreimpl<K, V> KeyValueStore for InMemoryStore<K, V>where K: std::cmp::Eq + std::hash::Hash, // Required for HashMap keys{ 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: Hash + Eq, 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 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, 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 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 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: 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<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, _:Self::Key, _:Self::Value); fn get(&self, _:&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<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;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);}
// 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>where K : std::cmp::Eq + std::hash::Hash{ pub storage: std::collections::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, 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: std::collections::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, 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 : std::cmp::Eq + std::hash::Hash{ pub storage: std::collections::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, 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: std::collections::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, 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<Key, Value> { pub storage: HashMap<Key, Value>,}impl<Key, Value> KeyValueStore for InMemoryStore<Key, Value> where Key: Eq+Hash { type Key = Key; type Value = Value; fn set(&mut self, k: Key, v: Value) { self.storage.insert(k, v); } fn get(&self, k: &Key) -> Option<&Value> { self.storage.get(k) }}// 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<Key, Value> { pub storage: HashMap<Key, Value>}// 3. Implement the trait for InMemoryStoreimpl<Key: Eq + Hash,Value> InMemoryStore<Key,Value> { pub fn set(&mut self, key: Key, value: Value) { self.storage.insert(key, value); } pub 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;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: K, value: V) { self.storage.insert(key, value); } fn get(&self, key: &K) -> Option<&V> { self.storage.get(key) // match self.storage.get(key) { // Some(value) => Some(value.cloned()), // None => None // } }}// 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 K; type V; fn set(&mut self, key: Self::K, value: Self::V); fn get(&self, key: &Self::K) -> Option<&Self::V>;}pub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>,}impl<K, V> InMemoryStore<K, V>where K: Eq + Hash,{ pub fn set(&mut self, key: K, value: V) { self.storage.insert(key, value); } pub 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;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> { pub storage: HashMap<Key, Value>,}impl<Key, Value> InMemoryStore<Key, Value>where Key: Eq + Hash,{ pub fn set(&mut self, key: Key, value: Value) { self.storage.insert(key, value); } pub fn get(&self, key: &Key) -> Option<&Value> { self.storage.get(key) }}
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> { pub storage: HashMap<Key, Value>,}impl<Key, Value> Default for InMemoryStore<Key, Value> { fn default() -> Self { InMemoryStore { storage: HashMap::new(), } }}impl<Key, Value> InMemoryStore<Key, Value>where Key: Eq + Hash,{ pub fn new() -> Self { InMemoryStore::default() } pub fn set(&mut self, key: Key, value: Value) { self.storage.insert(key, value); } pub fn get(&self, key: &Key) -> Option<&Value> { self.storage.get(key) }}
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: 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;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;use core::hash::Hash;// 1. Finish the trait definitionpub trait KeyValueStore{ type Key; type Value; fn set(&mut self, _: Self::Key, _: Self::Value); fn get(&self, _: &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,U> KeyValueStore for InMemoryStore<T,U> where T:Hash+Eq{ 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;// 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 + std::hash::Hash{ 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<T, U> { pub storage: HashMap<T, U>}// 3. Implement the trait for InMemoryStoreimpl <T, U>KeyValueStore for InMemoryStore<T, U> where T: Eq + std::hash::Hash, { type Key = T; type Value = U; fn get(&self, key: &Self::Key) -> Option<&U> { 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: 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;// 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);}
// 1. Finish the trait definitionuse 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>{ pub storage: HashMap<K,V>}// 3. Implement the trait for InMemoryStoreimpl<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) }}// 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: 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) }}// 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: K, value: V) { self.storage.insert(key, value); } fn get(&self, key: &K) -> Option<&V> { return 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> 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, 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: 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<T, U> { pub storage: HashMap<T, U>,}impl<T, U> KeyValueStore for InMemoryStore<T, U> where T: Hash + Eq,{ type Key = T; type Value = U; fn set(&mut self, key: T, value: U) { self.storage.insert(key, value); } fn get(&self, key: &T) -> Option<&U> { 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<T, U> { pub storage: HashMap<T, U>,}// 3. Implement the trait for InMemoryStoreimpl<T, U> KeyValueStore for InMemoryStore<T, U>where T: Hash + Eq,{ 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;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<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, 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>}impl<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, 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;pub trait KeyValueStore { type Key; type Value; // Method to add a key-value pair fn set(&mut self, key: Self::Key, value: Self::Value); // Method to get a value by key fn get(&self, key: &Self::Key) -> Option<&Self::Value>;}// Define the struct InMemoryStore with public fieldspub struct InMemoryStore<K, V> { pub storage: HashMap<K, V>,}impl<K, V> InMemoryStore<K, V>where K: std::cmp::Eq + std::hash::Hash,{ // Constructor to create a new InMemoryStore pub fn new() -> Self { InMemoryStore { storage: HashMap::new(), } }}// Implement the KeyValueStore 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) }}// 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);}