Tipos de Errores Dinámicos
Sometimes we want to allow any type of error to be returned without writing our own enum covering all the different possibilities. The std::error::Error
trait makes it easy to create a trait object that can contain any error.
Speaker Notes
This slide should take about 5 minutes.
La función read_count
puede devolver std::io::Error
(de las operaciones de archivos) o std::num::ParseIntError
(de String::parse
).
Boxing errors saves on code, but gives up the ability to cleanly handle different error cases differently in the program. As such it’s generally not a good idea to use Box<dyn Error>
in the public API of a library, but it can be a good option in a program where you just want to display the error message somewhere.
Asegúrate de implementar el trait std::error::Error
al definir un tipo de error personalizado para que pueda tener una estructura box. Sin embargo, si necesitas el atributo no_std
, ten en cuenta que el trait std::error::Error
de momento solo es compatible con no_std
en nightly.