[Bug] [Temperatures] [Rust] Error in parse_input macro (unwrap on an Err value)

Test Case 06 (No temperature) in the Temperatures level.

No integer input is passed to the program -> the parse macro tries to parse an empty string into an integer and unwraps on the Err value.

Faulty impl:

macro_rules! parse_input {
($x:expr, $t:ident) => ($x.trim().parse::<$t>().unwrap())

Console output:

Standard Output Stream:
thread ‘main’ panicked at ‘called Result::unwrap() on an Err value: ParseIntError { kind: Empty }’, /checkout/src/libcore/result.rs:906:4
stack backtrace:
0: std::sys:: imp::backtrace::tracing:: imp::unwind_backtrace
at /checkout/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
1: std::sys_common::backtrace::_print
at /checkout/src/libstd/sys_common/backtrace.rs:71
2: std::panicking::default_hook::{{closure}}
at /checkout/src/libstd/sys_common/backtrace.rs:60
at /checkout/src/libstd/panicking.rs:381
3: std::panicking::default_hook
at /checkout/src/libstd/panicking.rs:397
4: std::panicking::rust_panic_with_hook
at /checkout/src/libstd/panicking.rs:611
5: std::panicking::begin_panic
at /checkout/src/libstd/panicking.rs:572
6: std::panicking::begin_panic_fmt
at /checkout/src/libstd/panicking.rs:522
7: rust_begin_unwind
at /checkout/src/libstd/panicking.rs:498
8: core::panicking::panic_fmt
at /checkout/src/libcore/panicking.rs:71
9: core::result::unwrap_failed
at /checkout/src/libcore/macros.rs:41
10: <core::result::Result<T, E>>::unwrap
at /checkout/src/libcore/result.rs:772
11: Answer::main
at ./Answer.rs:27
12: __rust_maybe_catch_panic
at /checkout/src/libpanic_unwind/lib.rs:99
13: std::rt::lang_start
at /checkout/src/libstd/panicking.rs:459
at /checkout/src/libstd/panic.rs:361
at /checkout/src/libstd/rt.rs:61
14: main
15: __libc_start_main
16: _start
Found: Nothing
Expected: 0

That’s correct. So you have to handle that case separately and don’t try to parse the empty string.

Sure, you could check inputs, careful though, it’s not empty, it has a newline character (LF).
Personally, I updated the given code and removed the unwrap from the macro and wrapped the parse_input! usages in a match.
The default implementation shouldn’t panic on any testcases though. The fix is easy enough and won’t confuse other more unexperienced rust users.

1 Like

It will be solved very soon. Thank you for reporting this!