当前位置:网站首页>Rust:命令行参数与环境变量操作
Rust:命令行参数与环境变量操作
2020-11-08 08:59:55 【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++ 数字、string和char*的转换
- C++学习——centos7上部署C++开发环境
- C++学习——一步步学会写Makefile
- C++学习——临时对象的产生与优化
- C++学习——对象的引用的用法
- C++编程经验(6):使用C++风格的类型转换
- Won the CKA + CKS certificate with the highest gold content in kubernetes in 31 days!
- C + + number, string and char * conversion
- C + + Learning -- capacity() and resize() in C + +
- C + + Learning -- about code performance optimization
猜你喜欢
-
C + + programming experience (6): using C + + style type conversion
-
Latest party and government work report ppt - Park ppt
-
在线身份证号码提取生日工具
-
Online ID number extraction birthday tool
-
️野指针?悬空指针?️ 一文带你搞懂!
-
Field pointer? Dangling pointer? This article will help you understand!
-
HCNA Routing&Switching之GVRP
-
GVRP of hcna Routing & Switching
-
Seq2Seq实现闲聊机器人
-
【闲聊机器人】seq2seq模型的原理
随机推荐
- LeetCode 91. 解码方法
- Seq2seq implements chat robot
- [chat robot] principle of seq2seq model
- Leetcode 91. Decoding method
- HCNA Routing&Switching之GVRP
- GVRP of hcna Routing & Switching
- HDU7016 Random Walk 2
- [Code+#1]Yazid 的新生舞会
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- HDU7016 Random Walk 2
- [code + 1] Yazid's freshman ball
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- Qt Creator 自动补齐变慢的解决
- HALCON 20.11:如何处理标定助手品质问题
- HALCON 20.11:标定助手使用注意事项
- Solution of QT creator's automatic replenishment slowing down
- Halcon 20.11: how to deal with the quality problem of calibration assistant
- Halcon 20.11: precautions for use of calibration assistant
- “十大科学技术问题”揭晓!|青年科学家50²论坛
- "Top ten scientific and technological issues" announced| Young scientists 50 ² forum
- 求反转链表
- Reverse linked list
- js的数据类型
- JS data type
- 记一次文件读写遇到的bug
- Remember the bug encountered in reading and writing a file
- 单例模式
- Singleton mode
- 在这个 N 多编程语言争霸的世界,C++ 究竟还有没有未来?
- In this world of N programming languages, is there a future for C + +?
- es6模板字符
- js Promise
- js 数组方法 回顾
- ES6 template characters
- js Promise
- JS array method review
- 【Golang】️走进 Go 语言️ 第一课 Hello World
- [golang] go into go language lesson 1 Hello World