temp-env

Set environment variables temporarily for the duration of a closure, mainly for Rust tests.

Library
Cargo
v0.3.6
46 stars
MIT OR Apache-2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
23 /100 Needs Attention
Development Activity 0
Maintenance 0
Community 24
Maturity 56
Momentum 12

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
78 /100 Good
Architecture 76
Code Quality 82
Innovation 66
Learning Curve 90

temp-env is a small Rust crate for setting environment variables temporarily while a closure runs, then restoring the previous state afterward. It is primarily useful for testing code whose behavior depends on environment variables, letting each test run with its own configuration without interfering with other tests.

It provides helpers to set one or many variables (or explicitly unset them), returning the closure’s value once it completes. Because environment variables are process-global, the crate serializes access with a lock and offers async variants for use with futures-based test code.

What You Get

  • with_var to set or unset a single environment variable around a closure.
  • with_vars to set or unset multiple variables at once, each optionally None to unset.
  • Return-value passthrough so you can compute and capture a value inside the temporary environment.
  • Automatic restoration of the prior environment state after the closure finishes.
  • Optional async closure support via the async_closure feature for futures-based tests.

Common Use Cases

  • Testing configuration loading code under different environment-variable values.
  • Ensuring a test that mutates env vars does not leak state into other tests during cargo test.
  • Exercising both the present and absent cases of an environment variable in isolated tests.

Under The Hood

Architecture - The entire crate lives in a single src/lib.rs (~540 lines including tests). The core pattern captures the current values of the target variables, applies the requested set/unset operations, runs the user’s closure, then restores the captured state in all exit paths. A parking_lot mutex serializes these mutations because the process environment is global shared state and concurrent test threads would otherwise race.

Tech Stack - Rust 2021 edition (MSRV 1.63) with a single runtime dependency on parking_lot for the lock, plus an optional futures dependency gated behind the async_closure feature. tokio is used only as a dev-dependency for async tests.

Code Quality - Despite its small size, the crate is thoroughly tested: the file contains an extensive in-module test suite covering single/multiple variables, unsetting, return values, panics, and async closures. Restoration is handled carefully so state is not leaked even if the closure panics, and the code is idiomatic and well documented.

API Design - The API is minimal and highly ergonomic — with_var(key, value, closure) and with_vars([...], closure) read almost like English, and returning the closure’s value keeps call sites concise. Using Option<&str> for the value elegantly expresses both setting and unsetting, so the getting-started boilerplate is essentially nil.

Used by 5 apps in this directory

Rust
80%
MIT

fabro

Developer Tools · Devops

1,658

Define AI agent workflows as code graphs, route tasks across any LLM, and intervene only where it matters.

View details
82
Repo Health
83
Technical
67
Dependency
Built with
Rust 80%
TypeScript 15%
Updated 1 weeks ago
Rust
65%
Other

GitButler

AI Development · Developer Tools · Devops

21,726

Git, but better — a modern version control client with stacked branches, parallel workflows, unlimited undo, and first-class support for AI-powered development.

View details
89
Repo Health
84
Technical
66
Dependency
Built with
Rust 65%
TypeScript 22%
Svelte 10%
Updated 1 weeks ago
Rust
73%
Other

iii

Developer Tools · Devops

18,814

Compose, extend, and observe every backend service in real time using three primitives: Workers, Functions, and Triggers.

View details
87
Repo Health
85
Technical
65
Dependency
Built with
Rust 73%
TypeScript 16%
Updated 1 weeks ago
Rust
100%
Other

Meilisearch

Search

59,424

Lightning-fast hybrid search engine with AI-powered semantic and full-text retrieval for modern applications.

View details
89
Repo Health
88
Technical
68
Dependency
Built with
Rust 100%
Updated 2 weeks ago
Rust
85%
Apache 2.0

OpenShell

AI Agents · Developer Tools

8,811

The safe, private runtime that lets autonomous AI agents operate in sandboxed environments governed by declarative YAML policies — blocking data exfiltration, credential leaks, and unauthorized network activity before they happen.

View details
85
Repo Health
81
Technical
68
Dependency
Built with
Rust 85%
Updated 1 weeks ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers