当前位置:网站首页>Rust:命令行参数与环境变量操作
Rust:命令行参数与环境变量操作
2020-11-08 08:59:00 【osc_sejhgcp0】
Rust中,经常碰到命令行参数和环境变量操作。下面主要来了解一下。
一、命令行参数

从上面可以看到,std::env实现了从cargo run 命令行中提取命令行参数 a b c进入程序。
二、读取环境变量
std::env::var函数,实现了操作系统中环境变量的功能。
env::var()-> std::env::Vars
Vars是什么,很复杂,他实现了Iterator.以下是标准库中源码:
#[stable(feature = "env", since = "1.0.0")]
pub struct Vars {
inner: VarsOs,
}
/// An iterator over a snapshot of the environment variables of this process.
///
/// This structure is created by the [`std::env::vars_os`] function. See
/// its documentation for more.
///
/// [`std::env::vars_os`]: vars_os
#[stable(feature = "env", since = "1.0.0")]
pub struct VarsOs {
inner: os_imp::Env,
}
需要说明的是,环境变量中,字符并不一定是Unicode标准的,这样就会报错。这时可以用:
env::vars_os
再往下:读取特定环境变量操作
use std::env;
fn main() {
let args: Vec<String> = env::args().collect();
//println!("get command args :{:?} ", args);
//println!("get env args : ");
for (key, value) in env::vars() {
//println!(" {} => {}", key, value);
}
let key = "PATH";
match env::var(key) {
Ok(val) => {
// val is String, splited by ";"
println!("val =>{}",val);
},
Err(e) => println!("couldn't interpret {}: {}", key, e),
}
}
三、设置环境变量
一般包括:查看所有环境变量、设置新的环境变量和修改老的环境变量。
1、在cmd下


上面分别做了设置,查看单独环境变量操作、修改操作。还是比较简单。
2、powshell
#查看所有环境变量 ls env:
#搜索环境变量 ls env:NODE*
#查看单个环境变量 $env:NODE_ENV
#添加/更新环境变量 $env:NODE_ENV=development
#删除环境变量 del evn:NODE_ENV
3、linux
修改bashrc文件,这种方法更为安全,它可以把使用这些环境变量的权限控制到用户级别,这里是针对某一特定的用户,如果你需要给某个用户权限使用这些环境变量,你只需要修改其个人用户主目录下的
.bashrc文件就可以了。vi ~/.bashrc
在下面添加:
Export PATH="$PATH:/NEW_PATH"
资料来源:
https://www.cnblogs.com/Joans/p/7760378.html
版权声明
本文为[osc_sejhgcp0]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4324904/blog/4707890
边栏推荐
- C语言I博客作业03
- How did Julia become popular?
- GoLand writes a program with template
- Adobe Prelude / PL 2020 software installation package (with installation tutorial)
- 蓝牙2.4G产品日本MIC认证的测试要求
- [original] about the abnormal situation of high version poi autosizecolumn method
- Mouse small hand
- Architect (November 2020)
- 将“光头”识别为“足球”,AI 摄像头如何犯的错?
- ASP.NET A complete solution based on exception handling in MVC
猜你喜欢

Experience the latest version of erofs on Ubuntu

Adobe Prelude /Pl 2020软件安装包(附安装教程)

Mate 40 series launch with Huawei sports health service to bring healthy digital life

Qt混合Python开发技术:Python介绍、混合过程和Demo

Visual studio 2015 unresponsive / stopped working problem resolution

数据科学面试应关注的6个要点

1.深入Istio:Sidecar自动注入如何实现的?

The most detailed usage guide for perconaxtradbcluster8.0

Is blazor ready to serve the enterprise?

shiyou的数值分析作业
随机推荐
Experience the latest version of erofs on Ubuntu
Fgagt: flow guided adaptive graph tracking
Astra: the future of Apache Cassandra is cloud native
Python loop distinction (while loop and for loop)
November 07, 2020: given an array of positive integers, the sum of two numbers equals N and must exist. How to find the two numbers with the smallest multiplication?
Test requirements for MIC certification of Bluetooth 2.4G products in Japan
Golang anonymous structure member, named structure member, inheritance, composition
[summary series] technical system of Internet server: high performance database index
技术人员该如何接手一个复杂的系统?
More than 50 object detection datasets from different industries
解决RabbitMQ消息丢失与重复消费问题
Windows subsystem Ubuntu installation
Which is more worth starting with the difference between vivos7e and vivos7
Littlest jupyterhub| 02 using nbgitpuller to distribute shared files
Is blazor ready to serve the enterprise?
Python learning Day1 -- Basic Learning
PCR and PTS calculation and inverse operation in TS stream
Swiper window width changes, page width height changes lead to automatic sliding solution
Learn Scala if Else statement
iOS上传App Store报错:this action cannot be completed -22421 解决方案