It returns a Result, of which you're seeing the Ok variant. This is Rust's way to say "hey, this function can fail".
It's not always possible to get the current path (and, in fact, a "current path" doesn't have to exist). For some examples of it failing:
- the current directory might have been deleted from disk
- a directory containing the current one might have been deleted
- you might not have permission to access the path to the current directory
- the OS may just dislike you (by which I mean it can fail in many other ways).
In all of these cases, instead of Ok containing the path, current_dir returns the Err variant of Result, containing a value representing which error happened.
You need to decide what you want to do in that case. To do so, you could:
matchover theResult, so you can do different things for the two cases- use one of
Results many helper methods, such asmap,unwrap_or, etc - use the
?operator to return the error if one happened (although you'll still have to handle it eventually - use
.unwrap()to simply crash the entire program in case of error.