I have a simple text file:
test.txt
my_email@domain.com
my_password
I would like to read each of these lines into a HashMap that is structured like:
use std::collections::HashMap;
use std::fs:File;
use std::io::BufReader;
fn main() -> Result<(), std::io::Error>
{
let file = File::open("test.txt")
.expect("Cant open the file. Check the path");
let buf_reader = BufReader::new(file);
// Now I need to extract the lines and place them in my hashmap appropriately.
// I was trying to read lines (just for testing) using:
// for line in buf_reader.lines()
// {
// println!("{}", line?);
// }
// But not sure if there is a better way to extract 0th and 1st // index from the .lines()
let my_map = HashMap::from([
("email", // Want my first line of test.txt to be here),
("pw", // want my second line of text.txt to be here)
]);
}
Edit I Think I figured out the line? error. I needed to annotate my main() result But regardless, would appreciate optimal way to perform the above operation. Read lines into maybe a vector? and use the indices to place in the HashMap