官术网_书友最值得收藏!

How it works...

With env::vars(), we can access an iterator over all the env var that were set for the current process at the time of execution [6]. This list is pretty huge though, as you'll see when running the code, and for the most part, irrelevant for us.

It's more practical to access a single env var with env::var() [26], which returns an Err if the requested var is either not present or doesn't contain valid Unicode. We can see this in action in line [21], where we try to print a variable that we just deleted.

Because your env::var returns a Result, you can easily set up default values for them by using unwrap_or_default. One real-life example of this, involving the address of a running instance of the popular Redis (https://redis.io/) key-value storage, looks like this:

redis_addr = env::var("REDIS_ADDR")
.unwrap_or_default("localhost:6379".to_string());

Keep in mind that creating an env var with env::set_var() [13] and deleting it with env::remove_var() [19] both only change the env var for our current process. This means that the created env var are not going to be readable by other programs. It also means that if we accidentally remove an important env var, the rest of the operating system is not going to care, as it can still access it.

主站蜘蛛池模板: 岳西县| 冀州市| 即墨市| 台东县| 高要市| 舒兰市| 梧州市| 桃源县| 揭东县| 仪陇县| 宁强县| 广饶县| 阿坝县| 桦川县| 咸宁市| 汝城县| 洪湖市| 中牟县| 乌什县| 普陀区| 应用必备| 镇安县| 南江县| 奈曼旗| 郓城县| 化隆| 富阳市| 高州市| 凌源市| 齐齐哈尔市| 高要市| 无为县| 海盐县| 益阳市| 隆林| 昭通市| 社旗县| 洪江市| 大田县| 启东市| 英吉沙县|