3

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

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Nick M
  • 73
  • 1
  • 8

1 Answers1

2

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.

Solomon Ucko
  • 5,724
  • 3
  • 24
  • 45