From 0edfa81f2f7ebf81e3734c3021dbed32f3a4ca67 Mon Sep 17 00:00:00 2001 From: scoobybejesus Date: Tue, 24 Sep 2019 12:02:22 -0400 Subject: [PATCH] Substituted Vec::with_capacity() where able. --- src/csv_export.rs | 32 ++++++++++++++++---------------- src/csv_import_accts_txns.rs | 4 ++-- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/csv_export.rs b/src/csv_export.rs index 5369c29..35aecd8 100644 --- a/src/csv_export.rs +++ b/src/csv_export.rs @@ -19,8 +19,8 @@ pub fn _1_account_sums_to_csv( acct_map: &HashMap ) { - let mut rows: Vec> = [].to_vec(); - let mut header: Vec = [].to_vec(); + let mut rows: Vec> = Vec::with_capacity(acct_map.len()); + let mut header: Vec = Vec::with_capacity(5); header.extend_from_slice(&[ "Account".to_string(), @@ -36,7 +36,7 @@ pub fn _1_account_sums_to_csv( for j in 1..=length { let acct = acct_map.get(&(j as u16)).unwrap(); - let mut row: Vec = [].to_vec(); + let mut row: Vec = Vec::with_capacity(5); let balance: String; let tentative_balance = acct.get_sum_of_amts_in_lots(); @@ -81,8 +81,8 @@ pub fn _2_account_sums_nonzero_to_csv( raw_acct_map: &HashMap ) { - let mut rows: Vec> = [].to_vec(); - let mut header: Vec = [].to_vec(); + let mut rows: Vec> = Vec::with_capacity(acct_map.len()); // more than needed... + let mut header: Vec = Vec::with_capacity(5); header.extend_from_slice(&[ "Account".to_string(), @@ -98,7 +98,7 @@ pub fn _2_account_sums_nonzero_to_csv( for j in 1..=length { let acct = acct_map.get(&(j as u16)).unwrap(); - let mut row: Vec = [].to_vec(); + let mut row: Vec = Vec::with_capacity(5); let raw_acct = raw_acct_map.get(&acct.raw_key).unwrap(); let name = raw_acct.name.to_string(); @@ -149,8 +149,8 @@ pub fn _3_account_sums_to_csv_with_orig_basis( acct_map: &HashMap ) { - let mut rows: Vec> = [].to_vec(); - let mut header: Vec = [].to_vec(); + let mut rows: Vec> = Vec::with_capacity(acct_map.len()); + let mut header: Vec = Vec::with_capacity(6); header.extend_from_slice(&[ "Account".to_string(), @@ -167,7 +167,7 @@ pub fn _3_account_sums_to_csv_with_orig_basis( for j in 1..=length { let acct = acct_map.get(&(j as u16)).unwrap(); - let mut row: Vec = [].to_vec(); + let mut row: Vec = Vec::with_capacity(6); let balance: String; let tentative_balance = acct.get_sum_of_amts_in_lots(); @@ -228,7 +228,7 @@ pub fn _4_transaction_mvmt_detail_to_csv( ) -> Result<(), Box> { let mut rows: Vec> = [].to_vec(); - let mut header: Vec = [].to_vec(); + let mut header: Vec = Vec::with_capacity(12); header.extend_from_slice(&[ "Date".to_string(), "Txn#".to_string(), @@ -285,7 +285,7 @@ pub fn _4_transaction_mvmt_detail_to_csv( gain_loss = d128!(0); } - let mut row: Vec = [].to_vec(); + let mut row: Vec = Vec::with_capacity(12); row.push(date); row.push("Txn ".to_string() + &tx_number); @@ -327,7 +327,7 @@ pub fn _5_transaction_mvmt_summaries_to_csv( ) -> Result<(), Box> { let mut rows: Vec> = [].to_vec(); - let mut header: Vec = [].to_vec(); + let mut header: Vec = Vec::with_capacity(12); header.extend_from_slice(&[ "Date".to_string(), @@ -441,7 +441,7 @@ pub fn _5_transaction_mvmt_summaries_to_csv( if let Some(term) = term_st { - let mut row: Vec = [].to_vec(); + let mut row: Vec = Vec::with_capacity(12); row.push(txn_date_string.clone()); row.push(tx_num_string.clone()); @@ -460,7 +460,7 @@ pub fn _5_transaction_mvmt_summaries_to_csv( } if let Some(term) = term_lt { - let mut row: Vec = [].to_vec(); + let mut row: Vec = Vec::with_capacity(12); row.push(txn_date_string); row.push(tx_num_string); @@ -503,7 +503,7 @@ pub fn _6_transaction_mvmt_detail_to_csv_w_orig( ) -> Result<(), Box> { let mut rows: Vec> = [].to_vec(); - let mut header: Vec = [].to_vec(); + let mut header: Vec = Vec::with_capacity(16); header.extend_from_slice(&[ "Date".to_string(), "Txn#".to_string(), @@ -571,7 +571,7 @@ pub fn _6_transaction_mvmt_detail_to_csv_w_orig( orig_gain_loss = d128!(0); } - let mut row: Vec = [].to_vec(); + let mut row: Vec = Vec::with_capacity(16); row.push(date); row.push("Txn ".to_string() + &tx_number); diff --git a/src/csv_import_accts_txns.rs b/src/csv_import_accts_txns.rs index aa32c9e..5d57f46 100644 --- a/src/csv_import_accts_txns.rs +++ b/src/csv_import_accts_txns.rs @@ -27,7 +27,7 @@ pub(crate) fn import_accounts( let header4: csv::StringRecord; // is_margin // A StringRecord doesn't accept the same range indexing we need below, so we create our own - let mut headerstrings: Vec = [].to_vec(); + let mut headerstrings: Vec = Vec::with_capacity(header1.len()); for element in header1.into_iter() { headerstrings.push(element.to_string()) @@ -134,7 +134,7 @@ pub(crate) fn import_transactions( let mut proceeds_parsed = 0f32; // Next, create action_records. - let mut action_records_map_keys_vec: Vec = [].to_vec(); + let mut action_records_map_keys_vec: Vec = Vec::with_capacity(2); let mut outgoing_ar: Option = None; let mut incoming_ar: Option = None; let mut outgoing_ar_num: Option = None;