-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkv.rs
executable file
·47 lines (42 loc) · 1.16 KB
/
kv.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::collections::HashMap;
/// The `KvStore` stores string key-value pairs.
///
/// Key-value paris are stored in a `HashMap` in memory and not persisted to disk.
///
/// Example:
///
/// ```rust
/// # use kvs::KvStore;
/// let mut store = KvStore::new();
/// store.set("key".to_owned(), "value".to_owned());
/// let val = store.get("key".to_owned());
/// assert_eq!(val, Some("value".to_owned()));
/// ```
#[derive(Default)]
pub struct KvStore {
store: HashMap<String, String>,
}
impl KvStore {
/// Create a `KvStore`.
pub fn new() -> Self {
KvStore {
store: HashMap::new(),
}
}
/// Sets the value of a string key to a string.
///
/// Overwrites previous value if the key already exists.
pub fn set(&mut self, key: String, value: String) {
self.store.insert(key, value);
}
/// Gets the string value of a given string key.
///
/// Returns `None` if the given key does not exist.
pub fn get(&mut self, key: String) -> Option<String> {
self.store.get(&key).cloned()
}
/// Remove a given key.
pub fn remove(&mut self, key: String) {
self.store.remove(&key);
}
}