Having a simple script here:
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
Upon failure, the
? operator is roughly equivalent to
You can make
env::current_dir() fail by deleting the current directory.
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?
unwrap() on a
Err causes a panic.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.