Rust programming, read command line parameters

Time:2021-6-1

Video address

Headline address:https://www.ixigua.com/i676544267458235648…
Station B address:https://www.bilibili.com/video/av78062009?…
Netease cloud classroom address:https://study.163.com/course/introduction….

GitHub address

GitHub address

Get command line parameters

Get the command line parameters through STD:: env. The example is as follows:

use std::env;

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

You can also print the number of parameters:

use std::env;

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

Saving parameter values into variables

use std::env;

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

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

This work adoptsCC agreementReprint must indicate the author and the link of this article

Linghu Yichong