• Rust学习日记(一)Cargo的使用


    前言:
    这是一个系列的学习笔记,会将笔者学习Rust语言的心得记录。
    当然,这并非是流水账似的记录,而是结合实际程序项目的记录,如果你也对Rust感兴趣,那么我们可以一起交流探讨,使用Rust来构建程序。

    注:本文中使用Rust都是在windows环境下,如果是macOS或者linux,其指令或有不同,请注意。

    Cargo的定义及使用

    Rust语言的官方文档中,将Cargo定义为rust的系统构建与程序包管理器。
    Cargo的原意是货物,在这里我们可以将其理解为货物管理,引申为程序语言设计时的管理工具,相当于我们对货物进行管理一样。
    作为初学者,你可能会有疑问,为什么一定要用Cargo命令呢?
    其实,要编译Rust的程序,并非一定要用Cargo,你可以这样来编译你的rust程序:

    > rustc main.rs
    > .\main.exe
    Hello, world!
    
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述
    “rustc”可以直接将.rs格式的程序编译为可执行文件,在windows环境,即exe文件。
    在这里插入图片描述
    对于简单的程序,使用rustc编译足矣,但随着学习的深入,项目的复杂化,rustc很显然就很难满足使用,因为项目变得大而复杂后,不光是编译,还要管理项目相关的其他文件,如配置信息,依赖项,等。
    而Rust提供了复杂项目所需的工具,即Cargo。对于Cargo的介绍,我们千言万语汇成一句话,Cargo可以使你的Rust项目使用起来更简单。

    1、Cargo新建项目

    $ cargo new hello_cargo
    $ cd hello_cargo
    
    • 1
    • 2

    在这里插入图片描述
    使用Cargo创建一个新项目,非常简单,会自动在当前文件夹下生成项目文件夹、主程序文件main.rs(src文件夹下)以及相关的配置文件Cargo.toml。

    2、Cargo构建和编译项目Cargo build、Cargo run

    PS E:\100rust\hello_world> cargo build
       Compiling hello_world v0.1.0 (E:\100rust\hello_world)
        Finished dev [unoptimized + debuginfo] target(s) in 1.48s
    
    • 1
    • 2
    • 3

    在这里插入图片描述
    Cargo build会自动构建项目,自动创建一个target文件夹,文件夹包含编译后的文件:
    在这里插入图片描述
    可以看到里面有个hello_world.exe文件,此时可以直接调用它,成功运行后输出helloworld。
    在这里插入图片描述
    但是,我们可以使用Cargo run来一键编译且运行:
    在这里插入图片描述
    3、Cargo check仅编译项目
    有一个常会遇到的场景是,我在编程序时只想检查项目,但并不想生成可执行文件,我的目的是确保没有错误或者bug。因为,通常编译且生成可执行文件,需要的时间会大于仅编译检查的时间,所以使用Cargo check会更快,如果你使用Cargo run当然也是可行的,但我相信你不会这么做。
    在这里插入图片描述
    以上是一个简单但完整的项目构成的过程,使用Cargo在hello world这样的项目中似乎有点大材小用,但Cargo是为了你构建大型项目准备的,你应该在学习Rust的初期,就直接使用Cargo,这对你以后管理rust项目非常重要。

    当然,Cargo的指令远不止以上那些。

       build, b    Compile the current package
        check, c    Analyze the current package and report errors, but don't build object files
        clean       Remove the target directory
        doc, d      Build this package's and its dependencies' documentation
        new         Create a new cargo package
        init        Create a new cargo package in an existing directory
        add         Add dependencies to a manifest file
        remove      Remove dependencies from a manifest file
        run, r      Run a binary or example of the local package
        test, t     Run the tests
        bench       Run the benchmarks
        update      Update dependencies listed in Cargo.lock
        search      Search registry for crates
        publish     Package and upload this package to the registry
        install     Install a Rust binary. Default location is $HOME/.cargo/bin
        uninstall   Uninstall a Rust binary
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    如Cargo clean,将清除target文件夹。
    在这里插入图片描述

  • 相关阅读:
    C++零碎记录(二)
    人工智能之Tensorflow编程模型
    面试官: B 树和 B+ 树有什么区别?
    带讲解的自行车租赁系统,可做毕设/课设
    第十章 异常
    SoloX:Android和iOS性能数据的实时采集工具
    第3章业务功能开发(用户访问项目)
    人工神经网络技术及应用,人工神经网络的优势
    雅思 四处刷题的记录(一)
    刷题记录:牛客NC24263[USACO 2018 Feb G]Directory Traversal
  • 原文地址:https://blog.csdn.net/normer123456/article/details/134180344