4 ms·
What are the cases when `try!` macro bettern than `?` operator? I'm a beginner to Rust so I don't know about that.
by lzutao 7y ago
What are the cases when `try!` macro bettern than `?` operator? I'm a beginner to Rust so I don't know about that.
- est31 7y agoOften 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 https://github.com/est31/rcgen
- lzutao 7y agoThanks through I still don't get it. I only know that you can get backtraces of errors by setting `RUST_BACKTRACE=1`.