Rust编程知识拾遗:Rust 编程,读取命令行参数

视频地址

头条地址:https://www.ixigua.com/i6765442674582356483
B站地址:https://www.bilibili.com/video/av78062009?p=1
网易云课堂地址:https://study.163.com/course/introduction.htm?courseId=1209596906#/courseDetail?tab=1

github地址

github地址

获取命令行参数

通过std::env来获取命令行参数,例子如下:

use std::env;

fn main() {
    let args: Vec = env::args().collect();
    println!("{:?}", args);
}

还可以打印参数个数:

use std::env;

fn main() {
    let args: Vec = env::args().collect();
    println!("size = {}", args.len());
    for argument in args {
        println!("{}", argument);
    }
}

将参数值保存进变量

use std::env;

fn main() {
    let args: Vec = env::args().collect();
    println!("size = {}", args.len());

    let a = &args[1];
    println!("a = {}", a);
    let b = &args[2];
    println!("b = {}", b);
}

你可能感兴趣的:(Rust编程,rust,rust编程,rust编程语言,rust编程视频,rust程序设计)