当前位置:网站首页>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
边栏推荐
- Python loop distinction (while loop and for loop)
- Macquarie Bank drives digital transformation with datastex enterprise (DSE)
- 在Ubuntu上体验最新版本EROFS
- [summary series] technical system of Internet server: high performance database index
- M 端软件产品设计思虑札记 - 知乎
- ASP.NET MVC下基于异常处理的完整解决方案
- print( 'Hello,NumPy!' )
- Codeforce算法题 | 你能想出解法,让你的基友少氪金吗?
- 进程、线程和协程的区别
- vivoy73s和荣耀30青春版的区别
猜你喜欢
什么你的电脑太渣?这几招包你搞定! (Win10优化教程)
Cloud alibabab notes come out, the whole network detailed explanation only this one hand is slow
ulab 1.0.0发布
在Ubuntu上体验最新版本EROFS
More than 50 object detection datasets from different industries
Blazor 准备好为企业服务了吗?
vivoY73s和vivoY70s的区别 vivoY73s和vivoY70s哪个值得入手
Swiper window width changes, page width height changes lead to automatic sliding solution
Cloud Alibabab笔记问世,全网详解仅此一份手慢无
China Telecom announces 5g SA commercial scale in 2020
随机推荐
Cloud Alibabab笔记问世,全网详解仅此一份手慢无
China Telecom announces 5g SA commercial scale in 2020
Experience the latest version of erofs on Ubuntu
Julia 是如何风靡起来的?
GET,POST,PUT,DELETE,OPTIONS用法与说明
Mate 40 series launch with Huawei sports health service to bring healthy digital life
Golang anonymous structure member, named structure member, inheritance, composition
OSChina 周日乱弹 —— 之前呢,我一直以为自己是个……
5g/4g工业无线路由器
鼠标变小手
UCGUI简介
Mate 40系列发布 搭载华为运动健康服务带来健康数字生活
进程、线程和协程的区别
Swiper window width changes, page width height changes lead to automatic sliding solution
学习Scala IF…ELSE 语句
Abnormal + Abstract
Spotify是如何推动数据驱动决策的?
Shiyou's numerical analysis assignment
Brief history of computer
ASP.NET A complete solution based on exception handling in MVC