Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set environment variables by Rust

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "how to set environment variables in Rust". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to set environment variables in Rust" can help you solve your doubts.

Rust is an efficient and reliable universal high-level language. Its efficiency is not limited to development efficiency, its execution efficiency is also commendable, and it is a rare language that takes into account both development efficiency and execution efficiency.

The characteristics of Rust language

High performance-Rust is amazing and memory utilization is extremely high. Without runtime and garbage collection, it is competent for performance-demanding services, can run on embedded devices, and can easily integrate with other languages.

Reliability-Rust's rich type system and ownership model ensures memory safety and thread safety, allowing you to eliminate all kinds of errors at compile time.

Productivity-Rust has excellent documentation, friendly compilers, and clear error messages, integrates best-in-class tools-package managers and build tools, multi-editor support for intelligent automatic completion and type checking, automatic formatting code, and more.

In a project, we usually need to set some environment variables to store some credentials or other data, so we can use dotenv as a crate.

First add the dependency of dotenv to the project:

For example, in the following project, you need to set two environment variables, the database connection string and the Debug level. In the development environment, we can create the .env file under the project root:

In the .env file, we set two environment variables, DB_URL and LOG_LEVEL:

Let's go to main.rs. To access the environment variables of the system, we can use the std::env of the standard library:

Here, we first traverse and print the obtained environment variables, and then try to print the values of the three environment variables PATH, DB_URL, and LOG_LEVEL, respectively.

The results are as follows:

We can see that the environment variable is traversed in front, and the value of PATH is printed out later. Then the program panicked when it tried to get the DB_URL value because the environment variable was not found.

Now let's use the crate of dotenv:

Import dotenv, and then execute the dotenv () function at the beginning of the program, which loads environment variables from the .env file in the current directory or parent directory.

If you want to specify a different path, you can use the from_filename or from_path functions provided in crate.

Okay, so why call the ok () method after calling dotenv ()?

First, dotenv () returns a Result type, and a warning is issued if the returned value is not used:

After calling ok (), Result is converted to Option, and Option does not generate a warning that Result is not being used.

So why not use unwrap ()?

Because you don't use the .env file in a production environment, you should use real environment variables, and the dotenv () function will fail to load, and if you use unwrap (), your program will stop running.

So the purpose of using ok () here is to ignore errors when loading dotenv environment files fails.

Code:

Use std::env;use dotenv::dotenv;fn main () {dotenv (). Ok (); for (k, v) in env::vars () {println! ("{}: {}", k, v);} println! ("PATH: {}", env::var ("PATH"). Unwrap (); println! ("DB: {}", env::var ("DB_URL"). Unwrap ()) Println! ("LOG: {}", env::var ("LOG_LEVEL"). Unwrap ();} here, this article "how to set environment variables in Rust" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it to understand it. If you want to know more about related articles, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report