Hacker News new | ask | show | jobs
by lzutao 2589 days ago
What are the cases when `try!` macro bettern than `?` operator? I'm a beginner to Rust so I don't know about that.
1 comments

Often when you are getting error messages you would like to find the place they originate. Failure provides backtraces but not everyone uses failure and tbh it's quite a heavy dependency, using serde and so on. With try!, you can easily shadow the macro to e.g. emit a panic. This can be done to basically any codebase using try!, even if it's not yours. Migrating it to use failure is much harder :).

It would be cool if you just had a compiler flag that invokes an user settable handler function every time Err() gets constructed or something like that.

To give concrete examples, I was developing a library to generate certificates [1]. Often you generate a wrong one though and the parser library you use for testing gives you a Err(BadDer). If that's everything you are getting it can be a bit tough to find out the origins. As it was using ? I had to grep for all the places returning a BadDer error and replace them with a panic. Shadowing try! would have been much easier.

[1]: https://github.com/est31/rcgen

Thanks through I still don't get it. I only know that you can get backtraces of errors by setting `RUST_BACKTRACE=1`.
That only gives you backtraces for panics, but not for errors based on the Result<,> type.
I am not serious. Often in these cases I would use printf debugging to know where the errors came.

(I am still a Rust beginner).