Prettier messages
You might notice that the error messages printed by default by main
are quite ugly. This is due to the default Debug
implementation for the error types.
Fortunately we can do better by using the anyhow
crate. This crate adds a anyhow::Error
type which is compatible with all errors implementing the std::error::Error
trait.
Exercise 6.a: after adding the anyhow
crate to your Cargo.toml
(you should know how to do it by then), modify your main()
function so that it returns a Result<(), anyhow::Error>
.
Note that in anyhow
you will find the following definition
#![allow(unused)] fn main() { type Result<T> = std::result::Result<T, anyhow::Error>; }
(std::result::Result
being the "default" Result
type that we use)
It means that you can use anyhow::Result<T>
everywhere you would be using Result<T, anyhow::Error>
, as a shortcut.
Exercise 6.b: make your main()
function return a anyhow::Result<()>
.
Look at the produced errors. Aren't they beautiful, compared to what we had before?