Today I was trying to write polymorphic error handling in Rust, something achievable with dynamic_cast
in C++, and incredibly easy in Go. In Rust, of course, it’s overcomplicated.
After a couple frustrating hours of searching with no success, I stumbled upon the downcast_ref
method of the Error
trait, which finally allowed me to write what I wanted. Oddly enough, I couldn’t find this solution anywhere.
Note that the String
concrete type cannot be retrieved.
Playground link here.