Implicit (or syntactically-cheap) unwrap when scripting in Rust
⚓ Rust 📅 2026-01-13 👤 surdeus 👁️ 2I'm looking for a simple way to unwrap when writing "script-like" programs in Rust. There's essentially 3 options:
- All functions provide a panicking syntax. This is not realistic and only exists for common cases like indexing (with the
x[i]syntax) and arithmetic (with the usual mathematical syntax likex + y, at least in debug mode). But for functions likefs::read()there's nothing (and I'm not claiming it should). - There's a
PanicErrortype that is uninhabited and implementsFrom<T>for allTby panicking with the input error as message (for example withErr(input).unwrap()). This lets users use the?syntax and have all script functions (most often that's onlymain()) returnResult<T, PanicError>instead ofT. - There's a macro that just does
.unwrap()trading 9 characters for 4 (assuming a single letter character macro).
Is there already a library that provides either the second or third solution?
Also am I missing something? I don't think I'm the only one that doesn't want to write .unwrap() all the time when using Rust instead of Shell? My problem with anyhow is that it loses the stacktrace (in particular the line of the error), but maybe there's a way to configure it that I'm not aware of?
I guess this kind of library would be useful for things like cargo script too (in particular for stuff under std::env and std::fs).
2 posts - 2 participants
🏷️ Rust_feed