Skip to content

Instantly share code, notes, and snippets.

@rjgonza
Last active Aug 8, 2021
Embed
What would you like to do?
LearningRust
fn main() {
let mut args = std::env::args().skip(1);
let key = args.next().unwrap();
let value = args.next().unwrap();
let db = Database::new()?;
let contents = format!("{}\t{}\n", key, value);
println!("Key: {}, Value: {}", key, value);
db.insert(key, value);
}
use std::collections::HashMap;
struct Database {
inner: HashMap<String, String>,
}
impl Database {
fn new() -> Result<Database, std::io::Error> {
let contents = std::fs::read_to_string("kb.db")?;
let mut inner = HashMap::new();
for line in contents.lines() {
let chunks: Vec<&str> = line.split('\t').collect();
if chunks.len() != 2 {
todo!("Return error");
}
let key = chunks[0];
let value = chunks[1];
inner.insert(key.to_owned(), value.to_owned());
}
Ok(Database { inner: inner })
}
fn insert(&self, k: String, v: String) -> Result<Database, std::io::Error> {
let mut inner = HashMap::new();
inner.insert(k, v);
Ok(Database { inner: inner })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment