From 7eed39ce36a9259a986dcf4de6c80c614a975505 Mon Sep 17 00:00:00 2001 From: kuuuube Date: Thu, 27 Jul 2023 17:07:44 -0400 Subject: [PATCH] Remove unnecessary cloning --- src/args_parser.rs | 24 ++++++++++++------------ src/modes/guessing.rs | 12 +++++------- src/utils.rs | 7 +++---- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/src/args_parser.rs b/src/args_parser.rs index 172f29c..469cd3e 100644 --- a/src/args_parser.rs +++ b/src/args_parser.rs @@ -14,19 +14,19 @@ pub fn parse_args() -> Option { match split[0].to_lowercase().as_str() { "--help" => {help_message(); return None}, "-h" => {help_message(); return None}, - "--mode" => {settings.mode = match safe_get_string(split, 1).as_str() {"generation" => Mode::Generation, "guessing" => Mode::Guessing, _ => Mode::Interactive}}, - "--format" => {settings.format_string = safe_get_string(split, 1)}, - "--prompt" => {settings.prompt = safe_get_string(split, 1)}, - "--range" => {settings.range = parse_range(safe_get_string(split, 1))}, - "--step" => {settings.raw_step = safe_get_string(split.clone(), 1); settings.step = safe_parse_f64(safe_get_string(split.clone(), 1)); if safe_get_string(split.clone(), 1).split(".").collect::>().len() == 2 {settings.step_decimal_len = safe_get_string(split, 1).len()}}, - "--step-type" => {settings.step_type = match safe_get_string(split, 1).as_str() {"multiply" => StepType::Multiply, "exponent" => StepType::Exponent, _ => StepType::Add}}, + "--mode" => {settings.mode = match safe_get_string(&split, 1).as_str() {"generation" => Mode::Generation, "guessing" => Mode::Guessing, _ => Mode::Interactive}}, + "--format" => {settings.format_string = safe_get_string(&split, 1)}, + "--prompt" => {settings.prompt = safe_get_string(&split, 1)}, + "--range" => {settings.range = parse_range(safe_get_string(&split, 1))}, + "--step" => {settings.raw_step = safe_get_string(&split, 1); settings.step = safe_parse_f64(safe_get_string(&split, 1)); if safe_get_string(&split, 1).split(".").collect::>().len() == 2 {settings.step_decimal_len = safe_get_string(&split, 1).len()}}, + "--step-type" => {settings.step_type = match safe_get_string(&split, 1).as_str() {"multiply" => StepType::Multiply, "exponent" => StepType::Exponent, _ => StepType::Add}}, "--precise" => {settings.precise = true}, - "--round" => {settings.round = safe_parse_i64(safe_get_string(split, 1))} - "--output" => {settings.output = safe_get_string(split, 1)}, - "--correct" => {settings.correct = safe_get_string(split, 1)}, - "--incorrect" => {settings.incorrect = safe_get_string(split, 1)}, + "--round" => {settings.round = safe_parse_i64(safe_get_string(&split, 1))} + "--output" => {settings.output = safe_get_string(&split, 1)}, + "--correct" => {settings.correct = safe_get_string(&split, 1)}, + "--incorrect" => {settings.incorrect = safe_get_string(&split, 1)}, "--weight" => {settings.weight = true}, - "--max-decimal" => {settings.max_decimal = safe_parse_usize(safe_get_string(split.clone(), 1))} + "--max-decimal" => {settings.max_decimal = safe_parse_usize(safe_get_string(&split, 1))} _ => {unknown_command_message(split[0]); return None} } @@ -48,7 +48,7 @@ fn parse_range(input: String) -> (String, String) { return (range_vec.get(0).unwrap_or(&"").to_string(), range_vec.get(1).unwrap_or(&"").to_string()); } -fn safe_get_string(input: Vec<&str>, index: usize) -> String { +fn safe_get_string(input: &Vec<&str>, index: usize) -> String { return input.get(index).unwrap_or(&"").to_string(); } diff --git a/src/modes/guessing.rs b/src/modes/guessing.rs index dd99c48..4a28fba 100644 --- a/src/modes/guessing.rs +++ b/src/modes/guessing.rs @@ -11,12 +11,11 @@ pub fn guessing_mode(settings: Settings) { let input_string; if settings.weight { - let range = settings.range.clone(); - let digit_start = range.0.len(); - let digits_end = range.1.len(); + let digit_start = settings.range.0.len(); + let digits_end = settings.range.1.len(); - let start_float = safe_parse_f64(range.0); - let end_float = safe_parse_f64(range.1); + let start_float = safe_parse_f64(&settings.range.0); + let end_float = safe_parse_f64(&settings.range.1); let digit = rand::thread_rng().gen_range(digit_start..=digits_end); @@ -38,8 +37,7 @@ pub fn guessing_mode(settings: Settings) { let number = rand::thread_rng().gen_range(minimum_number..=maximum_number); input_string = clean_f64_to_string(number, settings.max_decimal); } else { - let range = settings.range.clone(); - let number = rand::thread_rng().gen_range(safe_parse_f64(range.0)..=safe_parse_f64(range.1)); + let number = rand::thread_rng().gen_range(safe_parse_f64(&settings.range.0)..=safe_parse_f64(&settings.range.1)); input_string = clean_f64_to_string(number, settings.max_decimal) } diff --git a/src/utils.rs b/src/utils.rs index ab4cd3c..f484a1a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -18,8 +18,7 @@ pub fn bigdecimal_powf(x: BigDecimal, e: &BigDecimal) -> BigDecimal { let simplified_denominator = denominator / gcd; let whole_result = bigdecimal_powi(&x.round(50), &BigDecimal::from_u32(simplified_numerator).unwrap()).round(50); - let result = bigdecimal_root(BigDecimal::from_u32(simplified_denominator).unwrap(), whole_result.clone()); - return result; + return bigdecimal_root(BigDecimal::from_u32(simplified_denominator).unwrap(), whole_result); } else { return bigdecimal_powi(&x, &whole_value); } @@ -38,7 +37,7 @@ pub fn bigdecimal_powi(x: &BigDecimal, e: &BigDecimal) -> BigDecimal { let mut r = BigDecimal::from_str("1").unwrap(); let mut i = BigDecimal::zero(); while i < *e { - r *= x.clone(); + r *= x; i += 1; } return r; @@ -79,7 +78,7 @@ pub fn clean_f64_to_string(float: f64, rounding: usize) -> String { return rounded; } -pub fn safe_parse_f64(input: String) -> f64 { +pub fn safe_parse_f64(input: &str) -> f64 { return input.parse::().unwrap_or_default(); }