Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the current working directory with the least amount of dependencies possible?

I have a Rust application which works as a shell. I want to get the working directory the user is in using as little dependencies as possible.

I tried using

let path = env::current_dir();
print!("{} > ",path.display());

But it gives me the following error: method not found in `Result<PathBuf, std::io::Error>'

I tried running this in the rust playground and it worked alright, so I was wondering if any of you could help me with this

like image 841
Nick M Avatar asked Sep 17 '25 20:09

Nick M


1 Answers

You can use std::env::current_dir to get the current working directory.

You also need to use match, std::result::Result::unwrap, std::result::Result::expect, ?, or something else to handle the Err variant of the Result and access the value inside the Ok variant.

like image 139
Solomon Ucko Avatar answered Sep 19 '25 09:09

Solomon Ucko