Crate pushd

source ·
Expand description

This crate provides a Pushd type that temporarily changes the current directory.

When a Pushd struct is created it will call env::set_current_dir to change to the given directory. When the Pushd is dropped, it will change back to the original directory.

If the original directory doesn’t exist, this error is ignored, since this may be because the original directory was a temporary directory. All other errors during drop cause a panic by default, but panics can be disabled entirely by using the Pushd::new_no_panic constructor.


use pushd::Pushd;
use std::path::PathBuf;

fn in_directory(path: PathBuf) {
    // When the current function exits and this variable is dropped, the
    // current directory will revert back to whatever it was before this
    // `Pushd` was created.
    let _pd = Pushd::new(path);
    // ...


The Pushd may panic if it cannot change back to the original directory when it’s dropped. Use the new_no_panic constructor to prevent this.


  • A Pushd changes the current directory when it’s created and returns to the original current directory when it’s dropped.


  • Error is an enum containing the structured errors that can be returned by this module.