{"id":17875964,"url":"https://github.com/bnjjj/tmp_env","last_synced_at":"2025-10-18T02:29:32.581Z","repository":{"id":57669971,"uuid":"375285505","full_name":"bnjjj/tmp_env","owner":"bnjjj","description":"tmp_env is a crate which lets you create temporary environment and be automatically cleaned when not needed.","archived":false,"fork":false,"pushed_at":"2021-06-16T09:37:45.000Z","size":6,"stargazers_count":5,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-01T11:22:33.608Z","etag":null,"topics":["directory","environment","rust","tmp"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bnjjj.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-06-09T08:40:30.000Z","updated_at":"2023-11-19T04:16:56.000Z","dependencies_parsed_at":"2022-09-26T20:40:40.905Z","dependency_job_id":null,"html_url":"https://github.com/bnjjj/tmp_env","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bnjjj%2Ftmp_env","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bnjjj%2Ftmp_env/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bnjjj%2Ftmp_env/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bnjjj%2Ftmp_env/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bnjjj","download_url":"https://codeload.github.com/bnjjj/tmp_env/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244169207,"owners_count":20409680,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["directory","environment","rust","tmp"],"created_at":"2024-10-28T11:26:20.954Z","updated_at":"2025-10-18T02:29:32.473Z","avatar_url":"https://github.com/bnjjj.png","language":"Rust","readme":"# tmp_env\n\n[![Version](https://img.shields.io/crates/v/tmp_env.svg)](https://crates.io/crates/tmp_env)\n[![Documentation](https://docs.rs/tmp_env/badge.svg)](https://docs.rs/tmp_env)\n\ntmp_env is a crate which lets you create temporary environment and be automatically cleaned when not needed.\n\nFor example sometimes you need to change the current directory or set environment variables to launch a process but you don't need this temporary environment for the rest of your program.\nThen you will use `tmp_env` to create environment variable using `tmp_env::set_var` instead of `std::env::set_var` to get from `tmp_env::set_var` a datastructure which will automatically restore the\ncorresponding environment variable when dropped.\n\n## Install\n\nPut this dependency in your `Cargo.toml`\n\n```toml\ntmp_env = \"0.1\"\n```\n\n## Usage\n\n- To temporary change the current directory:\n\n```rust\n{\n    let _tmp_current_dir = tmp_env::set_current_dir(\"src\").expect(\"should set the new current_dir\");\n    let current_dir = std::env::current_dir().expect(\"cannot get current dir from std env\");\n    assert!(current_dir.ends_with(\"src\"));\n}\nlet current_dir = std::env::current_dir().expect(\"cannot get current dir from std env\");\nassert!(!current_dir.ends_with(\"src\"));\n```\n\n- To temporary set an environment variable:\n\n```rust\n{\n    let _tmp_env = tmp_env::set_var(\"TEST_TMP_ENV\", \"myvalue\");\n    assert_eq!(std::env::var(\"TEST_TMP_ENV\"), Ok(String::from(\"myvalue\")));\n}\n// Because guard `_tmp_env` is dropped then the environment variable is also automatically unset (not restored because no previous value was set)\nassert!(std::env::var(\"TEST_TMP_ENV\").is_err());\n```\n\n- To temporary create a directory\n\n```rust\n{\n    let tmp_dir = tmp_env::create_temp_dir().expect(\"cannot create temp dir\"); // When tmp_dir is dropped this temporary dir will be removed\n    assert!(std::fs::metadata(\u0026*tmp_dir).is_ok());\n}\n// The temporary directory is now removed\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbnjjj%2Ftmp_env","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbnjjj%2Ftmp_env","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbnjjj%2Ftmp_env/lists"}