当前位置:网站首页>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
边栏推荐
- iOS上传App Store报错:this action cannot be completed -22421 解决方案
- 5G+AR出圈,中国移动咪咕成第33届中国电影金鸡奖全程战略合作伙伴
- 技术人员该如何接手一个复杂的系统?
- 蓝牙2.4G产品日本MIC认证的测试要求
- Insight -- the application of sanet in arbitrary style transfer
- 学习Scala IF…ELSE 语句
- Review the cloud computing application scenarios you didn't expect (Part 1)
- Cloud alibabab notes come out, the whole network detailed explanation only this one hand is slow
- iOS 学习笔记二【cocopods安装使用和安装过程中遇到的问题及解决办法】【20160725更新】
- Littlest JupyterHub| 02 使用nbgitpuller分发共享文件
猜你喜欢
Learn Scala if Else statement
FORTRAN 77 reads some data from the file and uses the heron iteration formula to solve the problem
shiyou的数值分析作业
来自不同行业领域的50多个对象检测数据集
Review the cloud computing application scenarios you didn't expect (Part 1)
Improvement of rate limit for laravel8 update
来自不同行业领域的50多个对象检测数据集
洞察——风格注意力网络(SANet)在任意风格迁移中的应用
Littlest jupyterhub| 02 using nbgitpuller to distribute shared files
SQL Server 2008R2 18456错误解决方案
随机推荐
Basic concepts of computer network (5) basic principles of local area network
M-end software product design considerations - Zhihu
GoLand writes a program with template
Application of bidirectional LSTM in outlier detection of time series
5g + Ar out of the circle, China Mobile Migu becomes the whole process strategic partner of the 33rd China Film Golden Rooster Award
iOS 学习笔记二【cocopods安装使用和安装过程中遇到的问题及解决办法】【20160725更新】
IOS learning note 2 [problems and solutions encountered during the installation and use of cocopods] [update 20160725]
[original] about the abnormal situation of high version poi autosizecolumn method
面部识别:攻击类型和反欺骗技术
1.深入Istio:Sidecar自动注入如何实现的?
Astra: Apache Cassandra的未来是云原生
来自不同行业领域的50多个对象检测数据集
Template linked list learning
Visual studio 2015 unresponsive / stopped working problem resolution
Cloud Alibabab笔记问世,全网详解仅此一份手慢无
阅读心得:FGAGT: Flow-Guided Adaptive Graph Tracking
Ulab 1.0.0 release
python_scrapy_房天下
Summary of knowledge points of Jingtao project
Mate 40 series launch with Huawei sports health service to bring healthy digital life