- Rust Standard Library Cookbook
- Jan Nils Ferner Daniel Durante
- 214字
- 2021-08-27 19:45:07
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.
- 常用元器件的識別與檢測
- App Inventor移動應(yīng)用開發(fā)標準教程
- 知識定義的6G網(wǎng)絡(luò)通感算資源智能調(diào)度
- 無線電壓制技術(shù)及應(yīng)用
- 西門子工業(yè)網(wǎng)絡(luò)通信技術(shù)詳解
- 電子產(chǎn)品組裝技能演練
- 怎樣用萬用表檢測電子元器件
- Cadence Allegro 17.4電子設(shè)計速成實戰(zhàn)寶典
- 手機故障維修技巧與實例
- 被動雷達寬帶數(shù)字接收機技術(shù)
- 光波分復(fù)用系統(tǒng)與維護
- 5G+智能制造
- 現(xiàn)代廣播電視發(fā)射技術(shù)
- 初級音響師速成實用教程(第2版)
- 新型DVD機維修數(shù)據(jù)速查寶典