Implicit (or syntactically-cheap) unwrap when scripting in Rust

⚓ Rust    📅 2026-01-13    👤 surdeus    👁️ 2      

surdeus

I'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 like x + y, at least in debug mode). But for functions like fs::read() there's nothing (and I'm not claiming it should).
  • There's a PanicError type that is uninhabited and implements From<T> for all T by panicking with the input error as message (for example with Err(input).unwrap()). This lets users use the ? syntax and have all script functions (most often that's only main()) return Result<T, PanicError> instead of T.
  • 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

Read full topic

🏷️ Rust_feed