Complete these tasks to reinforce what you learned in this module.
Define an error enum TaskError with two variants: Io(std::io::Error) and Invalid(String). Use #[derive(Error)] and #[error(...)]. Implement or derive From<std::io::Error> for Io. Write a function that returns Result<(), TaskError> and returns Err(TaskError::Invalid("bad input".into())). In main, call it and print the error.
#[derive(Error)] and #[error("...")] on each variant. Use #[from] on the Io variant so ? can convert. Return Err(TaskError::Invalid(...)) from your function.// Task 1: thiserror enum. See tasks/TASKS.md.
fn main() {
todo!("Define TaskError with thiserror, return Err, print");
}
Write a function read_file(path: &str) -> anyhow::Result<String> that reads the file with std::fs::read_to_string and adds context with .with_context(|| format!("reading {}", path)) on error. In main, call it with a non-existent path and print the error (e.g. with eprintln!("{:?}", e)).
.with_context() on the Result from read_to_string to attach a message when the error is propagated.// Task 2: anyhow with context. See tasks/TASKS.md.
fn main() {
todo!("read_file with with_context, call with bad path");
}