From 2386ea012dc886127bf3774cd0f603a0936f4280 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20LUDWIG?= Date: Mon, 21 Aug 2023 22:45:05 +0200 Subject: [PATCH] fix: avoid usage of `.unwrap` in Rust solutions --- .../solutions/rust/function/src/main.rs | 17 ++++++++++++++--- .../solutions/rust/function/src/main.rs | 4 ++-- .../solutions/rust/function/src/main.rs | 2 +- .../solutions/rust/bubble-sort/src/main.rs | 7 +++++-- .../solutions/rust/function/src/main.rs | 7 +++++-- .../solutions/rust/insertion-sort/src/main.rs | 7 +++++-- .../solutions/rust/merge-sort/src/main.rs | 7 +++++-- templates/solution/rust/src/main.rs | 2 +- 8 files changed, 38 insertions(+), 15 deletions(-) diff --git a/challenges/ascii-art/solutions/rust/function/src/main.rs b/challenges/ascii-art/solutions/rust/function/src/main.rs index a222987..b55f462 100644 --- a/challenges/ascii-art/solutions/rust/function/src/main.rs +++ b/challenges/ascii-art/solutions/rust/function/src/main.rs @@ -55,7 +55,9 @@ fn main() { row_ascii.clear(); characters_ascii .get_mut(&characters[character_index]) - .unwrap() + .expect( + "Failed to get `characters[character_index]` key from `characters_ascii`.", + ) .push(row_ascii_value); character_index += 1; character_ascii_index = 0; @@ -69,11 +71,20 @@ fn main() { let mut characters_needed: Vec> = vec![]; for (_, character) in text.char_indices() { let mut character = character.to_uppercase(); - let mut character = character.next().unwrap(); + let mut character = character + .next() + .expect("Failed to get `character.next()` value from `character.to_uppercase()`."); if !characters_ascii.contains_key(&character) { character = '?'; } - characters_needed.push(characters_ascii.get(&character).unwrap().clone()); + characters_needed.push( + characters_ascii + .get(&character) + .expect( + "Failed to get `characters_ascii.get(&character)` key from `characters_ascii`.", + ) + .clone(), + ); } let mut text_ascii = String::new(); diff --git a/challenges/cakes-swerc-2020-2021/solutions/rust/function/src/main.rs b/challenges/cakes-swerc-2020-2021/solutions/rust/function/src/main.rs index 5f9a902..5fd893d 100644 --- a/challenges/cakes-swerc-2020-2021/solutions/rust/function/src/main.rs +++ b/challenges/cakes-swerc-2020-2021/solutions/rust/function/src/main.rs @@ -22,12 +22,12 @@ fn main() { .get(0) .expect("Couldn't get `quantity_per_cake`.") .parse() - .unwrap(); + .expect("Failed to convert `quantity_per_cake` as an `u32`."); let quantity_available: u32 = line_integers .get(1) .expect("Couldn't get `quantity_available`.") .parse() - .unwrap(); + .expect("Failed to convert `quantity_available` as an `u32`."); let cake_possible = quantity_available / quantity_per_cake; if let Some(value) = maximum_number_of_cake_possible { diff --git a/challenges/hello-world/solutions/rust/function/src/main.rs b/challenges/hello-world/solutions/rust/function/src/main.rs index cc7ce0c..fe10389 100644 --- a/challenges/hello-world/solutions/rust/function/src/main.rs +++ b/challenges/hello-world/solutions/rust/function/src/main.rs @@ -3,6 +3,6 @@ use std::io::{self, BufRead}; fn main() { let stdin = io::stdin(); for line in stdin.lock().lines() { - println!("Hello, {}!", line.unwrap()); + println!("Hello, {}!", line.expect("Failed to read `stdin` line.")); } } diff --git a/challenges/sorting-algorithms/solutions/rust/bubble-sort/src/main.rs b/challenges/sorting-algorithms/solutions/rust/bubble-sort/src/main.rs index cdf38e8..b04f2ed 100644 --- a/challenges/sorting-algorithms/solutions/rust/bubble-sort/src/main.rs +++ b/challenges/sorting-algorithms/solutions/rust/bubble-sort/src/main.rs @@ -4,8 +4,11 @@ fn main() { let mut numbers: Vec = Vec::new(); let stdin = io::stdin(); for line in stdin.lock().lines().skip(1) { - let line = line.unwrap(); - let number: i64 = line.trim().parse().unwrap(); + let line = line.expect("Failed to read `stdin` line."); + let number: i64 = line + .trim() + .parse() + .expect("Failed to convert `number` as an `i64`."); numbers.push(number); } bubble_sort(&mut numbers); diff --git a/challenges/sorting-algorithms/solutions/rust/function/src/main.rs b/challenges/sorting-algorithms/solutions/rust/function/src/main.rs index 22a9284..c65bb38 100644 --- a/challenges/sorting-algorithms/solutions/rust/function/src/main.rs +++ b/challenges/sorting-algorithms/solutions/rust/function/src/main.rs @@ -4,8 +4,11 @@ fn main() { let mut numbers: Vec = Vec::new(); let stdin = io::stdin(); for line in stdin.lock().lines().skip(1) { - let line = line.unwrap(); - let number: i64 = line.trim().parse().unwrap(); + let line = line.expect("Failed to read `stdin` line."); + let number: i64 = line + .trim() + .parse() + .expect("Failed to convert `number` as an `i64`."); numbers.push(number); } numbers.sort(); diff --git a/challenges/sorting-algorithms/solutions/rust/insertion-sort/src/main.rs b/challenges/sorting-algorithms/solutions/rust/insertion-sort/src/main.rs index e4e3dc0..10fb612 100644 --- a/challenges/sorting-algorithms/solutions/rust/insertion-sort/src/main.rs +++ b/challenges/sorting-algorithms/solutions/rust/insertion-sort/src/main.rs @@ -4,8 +4,11 @@ fn main() { let mut numbers: Vec = Vec::new(); let stdin = io::stdin(); for line in stdin.lock().lines().skip(1) { - let line = line.unwrap(); - let number: i64 = line.trim().parse().unwrap(); + let line = line.expect("Failed to read `stdin` line."); + let number: i64 = line + .trim() + .parse() + .expect("Failed to convert `number` as an `i64`."); numbers.push(number); } insertion_sort(&mut numbers); diff --git a/challenges/sorting-algorithms/solutions/rust/merge-sort/src/main.rs b/challenges/sorting-algorithms/solutions/rust/merge-sort/src/main.rs index 05dc3b8..46a780f 100644 --- a/challenges/sorting-algorithms/solutions/rust/merge-sort/src/main.rs +++ b/challenges/sorting-algorithms/solutions/rust/merge-sort/src/main.rs @@ -4,8 +4,11 @@ fn main() { let mut numbers: Vec = Vec::new(); let stdin = io::stdin(); for line in stdin.lock().lines().skip(1) { - let line = line.unwrap(); - let number: i64 = line.trim().parse().unwrap(); + let line = line.expect("Failed to read `stdin` line."); + let number: i64 = line + .trim() + .parse() + .expect("Failed to convert `number` as an `i64`."); numbers.push(number); } merge_sort(&mut numbers); diff --git a/templates/solution/rust/src/main.rs b/templates/solution/rust/src/main.rs index cc7ce0c..fe10389 100644 --- a/templates/solution/rust/src/main.rs +++ b/templates/solution/rust/src/main.rs @@ -3,6 +3,6 @@ use std::io::{self, BufRead}; fn main() { let stdin = io::stdin(); for line in stdin.lock().lines() { - println!("Hello, {}!", line.unwrap()); + println!("Hello, {}!", line.expect("Failed to read `stdin` line.")); } }