Reputation: 626
I am working on a csv read, the function is take a Hashmap, modify them and return filled data.
pub fn read_csv(
maps: &mut HashMap<String, (NaiveDate, f64, NaiveDate, f64)>,
) -> Result<&HashMap<String, (NaiveDate, f64, NaiveDate, f64)>, Box<dyn Error>> {
....
}
This code seems very "ugly" ..
Upvotes: 1
Views: 191
Reputation: 361839
If you take a mutable reference then you're modifying the map in place, so I wouldn't bother returning it. You could return Result<(), _>
with ()
signaling no return value on success.
pub fn read_csv(
map: &mut HashMap<String, (NaiveDate, f64, NaiveDate, f64)>,
) -> Result<(), Box<dyn Error>> {
todo!();
}
Alternatively, you could take ownership of the map and then return it at the end. This would be appropriate if you want the map to be unusable when there's an error.
pub fn read_csv(
map: HashMap<String, (NaiveDate, f64, NaiveDate, f64)>,
) -> Result<HashMap<String, (NaiveDate, f64, NaiveDate, f64)>, Box<dyn Error>> {
todo!();
}
If you don't like the repetition you could, as @Joe_Jingyu suggests, create a type alias.
pub type DateMap = HashMap<String, (NaiveDate, f64, NaiveDate, f64)>;
pub fn read_csv(map: DateMap) -> Result<DateMap, Box<dyn Error>> {
todo!();
}
Nitpick: There's only one map so perhaps map
, singular, would be a better variable name.
Upvotes: 2