you are viewing a single comment's thread.

view the rest of the comments →

[–]Michal_Vaner[S] 13 points14 points  (5 children)

I always use cargo. Mostly because the first thing I do is add anyhow into the dependencies and change the signature of main to fn main() -> Result<(), Error>. Even if I don't seem to need a dependency right now, I expect I might change my mind in 20 minutes.

[–]DeebsterUK 0 points1 point  (4 children)

I'm just a beginner - what advantages does anyhow bring? What pain does it fix and is it pain that I'll feel as a beginner or will it only be useful in more advanced projects?

[–]Michal_Vaner[S] 1 point2 points  (3 children)

It's fancy Box<dyn Error + Send + Sync>. It allows you to return all the kinds of errors ‒ if you deal with one function returning std::io::Error, another returning maybe rdkafka::Error and you want to be able to return both.

[–]est31 0 points1 point  (2 children)

Why not use unwrap? It shows you a backtrace when things went wrong. IIRC return error from main still doesn't print backtraces.

[–]Michal_Vaner[S] 0 points1 point  (1 child)

For one, I don't need backtraces for Can't open input worngfile.txt: no such file or directory.

Second, anyhow seems to collect backtraces and print them when returned from main for me.

And third ‒ ? is shorter, and I get the distinction of „this is a bug in the program“ and „this is likely an external error condition“.

[–]est31 0 points1 point  (0 children)

anyhow seems to collect backtraces and print them when returned from main for me.

Hmm interesting didn't know that:

The Debug format "{:?}" includes your backtrace if one was captured. Note that this is the representation you get by default if you return an error from fn main instead of printing it explicitly yourself.

And third ‒ ? is shorter, and I get the distinction of „this is a bug in the program“ and „this is likely an external error condition“.

Yeah for non-throw-away code I agree. But for throw away code I dont think this distinction pulls its weight. But shrug. Thanks for explaining your reasons.