当前位置:网站首页>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
边栏推荐
- GET,POST,PUT,DELETE,OPTIONS用法与说明
- OSChina 周日乱弹 —— 之前呢,我一直以为自己是个……
- Daily challenges of search engines_ 4_ External heterogeneous resources - Zhihu
- Windows下子系统Ubuntu安装
- The real-time display of CPU and memory utilization rate by Ubuntu
- 成功解决An error ocurred while starting the kernel
- Template linked list learning
- 来自不同行业领域的50多个对象检测数据集
- Wechat nickname Emoji expression, special expression causes the list not to be displayed, export excel error report and other problems solved!
- Test requirements for MIC certification of Bluetooth 2.4G products in Japan
猜你喜欢

Review the cloud computing application scenarios you didn't expect (Part 1)

C/C++编程笔记:C语言相比其他编程语言,有什么不一样的优势?

How can a technician take over a complex system?

QT hybrid Python development technology: Python introduction, hybrid process and demo

Distributed consensus mechanism

What? Your computer is too bad? You can handle these moves! (win10 optimization tutorial)

计算机网络基本概念(五)局域网基本原理

高并发,你真的理解透彻了吗?

What details does C + + improve on the basis of C

Python3.9的7个特性
随机推荐
Introduction to ucgui
“智能5G”引领世界,数位智能网优+5G能带来什么?
16.文件传输协议、vsftpd服务
nvm
ubuntu实时显示cpu、内存占用率
GET,POST,PUT,DELETE,OPTIONS用法与说明
C语言I博客作业03
Python3.9的7个特性
Distributed consensus mechanism
搜索引擎的日常挑战_4_外部异构资源 - 知乎
成功解决An error ocurred while starting the kernel
Template linked list learning
Simple use of future in Scala
How does spotify drive data-driven decision making?
python 循环区分(while循环和for循环)
Solve the problem of rabbitmq message loss and repeated consumption
Bili Bili common API
Review the cloud computing application scenarios you didn't expect (Part 1)
413【毕设课设】基于51单片机无线zigbee无线智能家居光照温湿度传输监测系统
Littlest JupyterHub| 02 使用nbgitpuller分发共享文件