Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Library to help supply environment variables in testing and development

License

NotificationsYou must be signed in to change notification settings

dotenv-rs/dotenv

Repository files navigation

CIcodecovCrates.io

Achtung! This is a v0.* version! Expect bugs and issues all around.Submitting pull requests and issues is highly encouraged!

Quotingbkeepers/dotenv:

Storingconfiguration in the environmentis one of the tenets of atwelve-factor app.Anything that is likely to change between deployment environments–such asresource handles for databases or credentials for external services–shouldbe extracted from the code into environment variables.

This library is meant to be used on development or testing environments inwhich setting environment variables is not practical. It loads environmentvariables from a.env file, if available, and mashes those with the actualenvironment variables provided by the operative system.

Usage

The easiest and most common usage consists on callingdotenv::dotenv when theapplication starts, which will load environment variables from a file named.env in the current directory or any of its parents; after that, you can just callthe environment-related method you need as provided bystd::os.

If you need finer control about the name of the file or its location, you canuse thefrom_filename andfrom_path methods provided by the crate.

dotenv_codegen provides thedotenv! macro, whichbehaves identically toenv!, but first tries to load a.env file at compiletime.

Examples

A.env file looks like this:

# a comment, will be ignoredREDIS_ADDRESS=localhost:6379MEANING_OF_LIFE=42

You can optionally prefix each line with the wordexport, which willconveniently allow you to source the whole file on your shell.

A sample project using Dotenv would look like this:

externcrate dotenv;use dotenv::dotenv;use std::env;fnmain(){dotenv().ok();for(key, value)in env::vars(){println!("{}: {}", key, value);}}

Variable substitution

It's possible to reuse variables in the.env file using$VARIABLE syntax.The syntax and rules are similar to bash ones, here's the example:

VAR=oneVAR_2=two# Non-existing values are replaced with an empty stringRESULT=$NOPE#value: '' (empty string)# All the letters after $ symbol are treated as the variable name to replaceRESULT=$VAR#value: 'one'# Double quotes do not affect the substitutionRESULT="$VAR"#value: 'one'# Different syntax, same resultRESULT=${VAR}#value: 'one'# Curly braces are useful in cases when we need to use a variable with non-alphanumeric nameRESULT=$VAR_2#value: 'one_2' since $ with no curly braces stops after first non-alphanumeric symbolRESULT=${VAR_2}#value: 'two'# The replacement can be escaped with either single quotes or a backslash:RESULT='$VAR'#value: '$VAR'RESULT=\$VAR#value: '$VAR'# Environment variables are used in the substutution and always override the local variablesRESULT=$PATH#value: the contents of the $PATH environment variablePATH="My local variable value"RESULT=$PATH#value: the contents of the $PATH environment variable, even though the local variable is defined

Dotenv will parse the file, substituting the variables the way it's described in the comments.

Using thedotenv! macro

Adddotenv_codegen to your dependencies, and add the following to the top ofyour crate:

#[macro_use]externcrate dotenv_codegen;

Then, in your crate:

fnmain(){println!("{}", dotenv!("MEANING_OF_LIFE"));}

About

Library to help supply environment variables in testing and development

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp