3 ms·
You often won't need to change the error object from what foo() returns; Rust errors are generally structs or enums, rather than non-machine-readable strings. B
by ameliaquining 10y ago
You often won't need to change the error object from what foo() returns; Rust errors are generally structs or enums, rather than non-machine-readable strings. But if you do need to change it, it looks like this:
foo().map_err(MyError::new)?
- cpuguy83 10y agogo errors are generally backed by structs as well (though not necessarily), even `fmt.Errorf("some error")` is a struct (called `errorString`). `Error() string` is just the interface that things that want to be errors must implement, which ideally gives you the human readable version of the error.
- saghm 10y agoIn Rust, I think it's more idiomatic to just implement the `Display` trait on you error type so that you can generate the string when you need it instead of at the time the error occurs