1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-12-08 00:45:29 +01:00

fix: avoid usage of .unwrap in Rust solutions

This commit is contained in:
Théo LUDWIG 2023-08-21 22:45:05 +02:00
parent 5b3048f305
commit 2386ea012d
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
8 changed files with 38 additions and 15 deletions

View File

@ -55,7 +55,9 @@ fn main() {
row_ascii.clear(); row_ascii.clear();
characters_ascii characters_ascii
.get_mut(&characters[character_index]) .get_mut(&characters[character_index])
.unwrap() .expect(
"Failed to get `characters[character_index]` key from `characters_ascii`.",
)
.push(row_ascii_value); .push(row_ascii_value);
character_index += 1; character_index += 1;
character_ascii_index = 0; character_ascii_index = 0;
@ -69,11 +71,20 @@ fn main() {
let mut characters_needed: Vec<Vec<String>> = vec![]; let mut characters_needed: Vec<Vec<String>> = vec![];
for (_, character) in text.char_indices() { for (_, character) in text.char_indices() {
let mut character = character.to_uppercase(); 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) { if !characters_ascii.contains_key(&character) {
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(); let mut text_ascii = String::new();

View File

@ -22,12 +22,12 @@ fn main() {
.get(0) .get(0)
.expect("Couldn't get `quantity_per_cake`.") .expect("Couldn't get `quantity_per_cake`.")
.parse() .parse()
.unwrap(); .expect("Failed to convert `quantity_per_cake` as an `u32`.");
let quantity_available: u32 = line_integers let quantity_available: u32 = line_integers
.get(1) .get(1)
.expect("Couldn't get `quantity_available`.") .expect("Couldn't get `quantity_available`.")
.parse() .parse()
.unwrap(); .expect("Failed to convert `quantity_available` as an `u32`.");
let cake_possible = quantity_available / quantity_per_cake; let cake_possible = quantity_available / quantity_per_cake;
if let Some(value) = maximum_number_of_cake_possible { if let Some(value) = maximum_number_of_cake_possible {

View File

@ -3,6 +3,6 @@ use std::io::{self, BufRead};
fn main() { fn main() {
let stdin = io::stdin(); let stdin = io::stdin();
for line in stdin.lock().lines() { for line in stdin.lock().lines() {
println!("Hello, {}!", line.unwrap()); println!("Hello, {}!", line.expect("Failed to read `stdin` line."));
} }
} }

View File

@ -4,8 +4,11 @@ fn main() {
let mut numbers: Vec<i64> = Vec::new(); let mut numbers: Vec<i64> = Vec::new();
let stdin = io::stdin(); let stdin = io::stdin();
for line in stdin.lock().lines().skip(1) { for line in stdin.lock().lines().skip(1) {
let line = line.unwrap(); let line = line.expect("Failed to read `stdin` line.");
let number: i64 = line.trim().parse().unwrap(); let number: i64 = line
.trim()
.parse()
.expect("Failed to convert `number` as an `i64`.");
numbers.push(number); numbers.push(number);
} }
bubble_sort(&mut numbers); bubble_sort(&mut numbers);

View File

@ -4,8 +4,11 @@ fn main() {
let mut numbers: Vec<i64> = Vec::new(); let mut numbers: Vec<i64> = Vec::new();
let stdin = io::stdin(); let stdin = io::stdin();
for line in stdin.lock().lines().skip(1) { for line in stdin.lock().lines().skip(1) {
let line = line.unwrap(); let line = line.expect("Failed to read `stdin` line.");
let number: i64 = line.trim().parse().unwrap(); let number: i64 = line
.trim()
.parse()
.expect("Failed to convert `number` as an `i64`.");
numbers.push(number); numbers.push(number);
} }
numbers.sort(); numbers.sort();

View File

@ -4,8 +4,11 @@ fn main() {
let mut numbers: Vec<i64> = Vec::new(); let mut numbers: Vec<i64> = Vec::new();
let stdin = io::stdin(); let stdin = io::stdin();
for line in stdin.lock().lines().skip(1) { for line in stdin.lock().lines().skip(1) {
let line = line.unwrap(); let line = line.expect("Failed to read `stdin` line.");
let number: i64 = line.trim().parse().unwrap(); let number: i64 = line
.trim()
.parse()
.expect("Failed to convert `number` as an `i64`.");
numbers.push(number); numbers.push(number);
} }
insertion_sort(&mut numbers); insertion_sort(&mut numbers);

View File

@ -4,8 +4,11 @@ fn main() {
let mut numbers: Vec<i64> = Vec::new(); let mut numbers: Vec<i64> = Vec::new();
let stdin = io::stdin(); let stdin = io::stdin();
for line in stdin.lock().lines().skip(1) { for line in stdin.lock().lines().skip(1) {
let line = line.unwrap(); let line = line.expect("Failed to read `stdin` line.");
let number: i64 = line.trim().parse().unwrap(); let number: i64 = line
.trim()
.parse()
.expect("Failed to convert `number` as an `i64`.");
numbers.push(number); numbers.push(number);
} }
merge_sort(&mut numbers); merge_sort(&mut numbers);

View File

@ -3,6 +3,6 @@ use std::io::{self, BufRead};
fn main() { fn main() {
let stdin = io::stdin(); let stdin = io::stdin();
for line in stdin.lock().lines() { for line in stdin.lock().lines() {
println!("Hello, {}!", line.unwrap()); println!("Hello, {}!", line.expect("Failed to read `stdin` line."));
} }
} }