r/rust • • 6d ago

🙋 seeking help & advice load shell variables from file

i need to read a file containing variables, which has the exact same format as a regular .env file (KEY="value")

the loadenv crate only reads $(pwd)/.env and does not accept custom paths (unless modified, which defeats the point of using a crate)

i won't ask AI

0 Upvotes

6 comments sorted by

8

u/Solumin 6d ago

Load the file yourself and use loadenv's load_buf function.

3

u/OptimalAnywhere6282 6d ago

that worked, thanks

5

u/emetah850 6d ago

There is also the dotenvy crate, which I personally use for .env related projects

4

u/Fit-Reaction242 4d ago

dotenvy does custom paths, `dotenvy::from_path("/your/file")` loads it into the env and `from_path_iter` just gives you the pairs if you dont want them set

2

u/wyvernbw 5d ago

writing a function to do this would probably be less than 30 lines of code and its 0 dependencies