这是一份不错的rust教程,目前包括4个block和4个project。全部完成后可以用rust实现一个简单的key-value存储引擎。

注意:Windows下rust貌似会遇到一些bug,强烈建议使用Linux来开发

 

Building Block1

一开始就是Hello World啦......通过实现一个简单的命令行程序来体验一下rust

比如我们希望程序能获得命令行参数

use std::env;

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

运行结果:
F:\My Drive\19fall\talent-plan\rust\building-blocks\bb1\src>main.exe 11 22
["main.exe", "11", "22"]

这一段看起来和c++差不多......(其实感觉rust比go好理解多了...)

  • println!结尾的叹号!表示调用了一个Rust宏。如果是调用函数,应该输入println

 

但是一个复杂的cli程序(比如Linux中的ls),命令行参数是很复杂的。比如我们想给写个help(比如ls -h)供用户参考,该怎么办呢?我们可以使用rust的clap库来实现。

首先需要定义一个yml,里面定义命令行参数的格式,保存为/src/cli.yml

name: myapp
version: "1.0"
author: Kevin K. <kbknapp@gmail.com>
about: Does awesome things
args:
    - config:
        short: c
        long: config
        value_name: configval
        help: Sets a custom config file
        takes_value: true
    - INPUT:
        help: Sets the input file to use
        required: true
        index: 1
    - verbose:
        short: v
        multiple: true
        help: Sets the level of verbosity
subcommands:
    - test:
        about: controls testing features
        version: "1.3"
        author: Someone E. <someone_else@other.com>
        args:
            - debug:
                short: d
                help: print debug information
View Code

相关文章:

  • 2022-02-21
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-04-23
  • 2022-12-23
  • 2021-08-22
  • 2022-12-23
  • 2021-04-12
  • 2022-01-25
  • 2021-11-22
相关资源
相似解决方案