First and foremost - I apologize. I'm a python guy and this is the first time I've used a system level language (and Jesus Christ its hard - Respect to all of you). Hope I'm in the right place, if not, please kindly point me in the direction of the newbie forums
I'm currently playing around with a Terminal UI library - I figure that tinkering around is the best way to learn.
The examples in that library all have hard coded data in the format of:
(Also my desired format)
const LOGS: [(&str, &str); 2] = [
("Event1", "INFO"),
("Event2", "INFO")]
My first goal/aim is to implement a function to read a CSV, assign it to a variable and then feed it into "LOGS" instead of the hardcoded values.
Now due to my years of python, I'm stuck in a way of happy go lucky thinking... where I can easily read a CSV file in and then pass it to a variable X that something else can use - Not that simple..
What I have so far (what I found on stackoverflow and modified):
extern crate csv;
use std::error::Error;
use csv::StringRecord;
#[derive(Debug)]
struct DataFrame {
name: Vec<String>,
}
impl DataFrame {
fn new() -> DataFrame {
DataFrame {
name: Vec::new(),
}
}
fn read_csv(filepath: &str, has_headers: bool) -> DataFrame {
// Open file
let file = std::fs::File::open(filepath).unwrap();
let mut rdr = csv::ReaderBuilder::new()
.has_headers(has_headers)
.from_reader(file);
let mut data_frame = DataFrame::new();
// push all the records
for result in rdr.records().into_iter() {
let record = result.unwrap();
data_frame.push(&record);
}
return data_frame;
}
fn push(&mut self, row: &csv::StringRecord) {
// get name
self.name.push(row[0].to_string());
}
}
fn main() {
let data = DataFrame::read_csv("./data.csv", true);
println!("{:?}", data)
}
With the data csv like so:
Name,
Please,
Work,
I get an output:
>DataFrame { name: ["Please", "Work"] }
So I'm kinda half way there...
This is where I'm stuck - Questions:
1.) How do I manipulate the read in to match my desired format? (I feel like its something to do with the struct).
2.) Is it possible to then pass it to a variable like I would in python e.g. x = someFunction(./filename) ?
3.) Lets assume I have read the csv in and its passed into a variable and now I can assign it to 'LOGS' - The 'LOGS' variable(constant) is hardcoded for 2 values. How do I make it dynamic to accept X records from my csv?