News for hackers. Stuff that matters.
Re:Four Years of Rust by lzutao on Thursday May 16, 2019 @07:22AM (#19927946)
What are the cases when `try!` macro bettern than `?` operator? I'm a beginner to Rust so I don't know about that.
Re:Four Years of Rust by est31 on Thursday May 16, 2019 @12:37PM (#19930501)
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

Re:Four Years of Rust by lzutao on Thursday May 16, 2019 @11:16PM (#19935915)
Thanks through I still don't get it. I only know that you can get backtraces of errors by setting `RUST_BACKTRACE=1`.
Re:Four Years of Rust by est31 on Friday May 17, 2019 @06:04AM (#19938009)
That only gives you backtraces for panics, but not for errors based on the Result<,> type.
Re:Four Years of Rust by lzutao on Friday May 17, 2019 @08:31AM (#19938857)
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).