A simple question regarding error handling

Having a simple script here:

playground

Since it is hard to simulate a failing env::current_dir() I'm wondering what the result of the entire function would be, in case it really fails. This in mind: Is there a way to willingly make such functions fail (for tests?)

Result of the entire function would still be exactly the type it returns. The Result would contain Box<dyn Error> created from an io::Error.

Upon failure, the ? operator is roughly equivalent to return Result::Err(error.into()).

You can make env::current_dir() fail by deleting the current directory.

Note that current_dir() is a poor API, and you should avoid it if you can. It's not reliable in a multi-threaded programs, because any thread can change it at any time affecting all other threads.

Thanks @kornel, interesting. I just need the current directory once at startup, so I suppose there is not much danger.

Nice trick - deleting the current directory. Will give it a try.

So in case it fails, an unchecked unwrap with panic in the calling function, right?

Yes, unwrap() on a Result with Err causes a panic.