#(Beginner) current_dir() returns more than path

34 messages · Page 1 of 1 (latest)

wild spoke
#

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:

  • match over the Result, so you can do different things for the two cases
  • use one of Results many helper methods, such as map, 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.
#

For more information, you want to look into what Result is and how it's used for error handling. The Book has some chapters on that, for example.

#

@lucid oar

blissful badge
#

It should also be noted that even if it does succeed, env::current_dir() doesn't have to have anything to do with the folder the executable is in. It can be completely different, as it's simply the working directory passed to the program when it was loaded by the Operating System. If you want specifically to get the path to the folder the executable is in, you should combine env::current_exe() with Path::join() and fs::canonicalize() instead

#

My source for this being the Rust Programming Language Forums; I don't know how you would combine them as the topic I looked at doesn't say

sonic vale
#

how is that not easy? You only need to handle the error case of what you get and you get the current path from Ok(...)

#

Are you expecting something else other than "C:\\Users\\Terra2\\Documents\\VSCode-Projects\\testing-getinfofromweb" ?

blissful badge
#

Result<> is Rust's way of saying that this operation can fail, and forcing you - enforcing it through the type system - to handle that possibility

blissful badge
wild spoke
#

We understand that's what you want to do, but the problem is, what do you want to do if there isn't a current path?

astral wyvern
#

The OS gives you an error instead of a path. What should your program do? Answer that question and we can tell you how to accomplish that.

wild spoke
#

env::current_dir().unwrap()

#

unwrap means "get the value inside the Ok, or panic if it's actually an Err"

blissful badge
#

You also have unwrap_err() which will return the error if it's an Err, or panic if it's an Ok

#

c:

wild spoke
#

Symmetry. it was probably easy to add 😄

astral wyvern
#

unwrap_err() is useful for tests where you want to check that a function does return an error

blissful badge
#

That's because .text() also returns a Result

#

Because it might fail

midnight narwhal
#

so if you want to panic: ```rs
let ipv4 = reqwest::get("https://api.ipify.org")
.unwrap()
.text()
.unwrap();

blissful badge
#

So you need to unwrap that as well, as seen above

midnight narwhal
#

However unwrapping all the time is a pain, it might be worth investing in proper error handling

blissful badge
#

^

midnight narwhal
#

the easiest way IMO is with Anyhow

#

Basically: make all your functions return an anyhow::Result

#

What version of reqwest are you using?

#

Reqwest hasn’t had that API for like…ages

#

uhhh

#

upgrade

#

to upgrade you just have to substitute reqwest::get for reqwest::blocking::get