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

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.

主站蜘蛛池模板: 瓦房店市| 和平区| 京山县| 闵行区| 平顺县| 桃园县| 许昌市| 杨浦区| 平昌县| 绍兴县| 佛坪县| 湖州市| 皋兰县| 文成县| 蒲城县| 西平县| 海盐县| 仙桃市| 出国| 天门市| 瑞丽市| 班戈县| 申扎县| 昭苏县| 道真| 东港市| 洛阳市| 桃源县| 灵宝市| 台山市| 始兴县| 长垣县| 长丰县| 喀喇| 中阳县| 阜宁县| 雅江县| 宜州市| 曲阜市| 肥城市| 江西省|