Struct inside a function with derive macro

Hi,

I am trying to write CSV serializer within a method. But faced with the following error. Is it possible to write do something like following ?

pub fn foo() -> Result<bool, io::Error> {
#[derive(Deserialize)]
struct t2g_pair {
    transcript_name: String,
    gene_name: String,
};

let mut rdr = csv::ReaderBuilder::new()
    .has_headers(false)
    .delimiter(b'\t')
    .from_reader(t2g_file.unwrap());
.
.
.
}

Currently facing the following error

error: cannot find derive macro `Deserialize` in this scope

You need to import Deserialize and turn on the serde derive macros.

[dependencies]
serde = { version = "1.0", features = ["derive"] }
use serde::Deserialize;

#[derive(Deserialize)]
struct T2gPair {
    transcript_name: String,
    gene_name: String,
};

There should be no issue with defining the struct inside a function.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.