Taking input from scv file

I am trying to take input from a csv file.

The following is my approach :

fn get_data() -> (Vec, Vec<Vec>) {

let mut data = Vec::new();
let mut headers = Vec::new();
let mut columns = Vec::new();

let mut csv_read = csv::Reader::from_reader(io::stdin());

for i in csv_read.deserialize() {
    let record: Record = i.unwrap();

let keys = csv_read.headers().unwrap();
for key in keys {

for key in keys.iter() {
    let mut each_col = Vec::new();
    for i in data.iter() {
        for (k, val) in i {
            if k == key {

(headers, columns)

But I am getting the following error :

(headers, columns)
| ^^^^^^^ expected struct std::string::String, found `&std::string::String

How do I change the return type ?
Or any other modifications...
please suggest !

Please read the following thread to help with formatting your code on the forum. It will help with readability: Forum Code Formatting and Syntax Highlighting

The issue is that when you iterate a container, it will yield borrowed elements by default. There are a few ways you can get it to yield owned values:

  1. Consume the container by transforming it into an iterator with the into_iter() method.
  2. Clone the elements as you iterate with the cloned() adapter for non-Copy types like String.
  3. Manually clone elements with a method like to_string() or to_owned() on the borrowed element.

In this case, you won't be able to consume the data container because it is iterated several times (you might want to adjust the shape of your code to iterate less). And using the cloned() adapter would be wasteful because you only use the yielded value conditionally.

So the easiest thing to do is option 3: each_col.push(val.to_owned()); will solve the compile error. Here it is in full context: playground (I took some liberties, like making up a Record type that matches the shape of your code, and also took an educated guess at the actual argument types; they were wiped out by bad formatting in your post.)

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.