如何用 Cargo 管理 Rust 工程系列 乙

以下内容为本人的学习笔记,如需要转载,请声明原文链接微信公众号「ENG八戒」mp.weixin.qq.com/s/__nvVZYti...

编译程序

这次我们用 cargo 来启动编译,cargo 提供了 build 指令来调度工具构建并输出软件。cargo build 只对当前目录或者向上查找目录的 Cargo.toml 文件有效,所以在启动构建前先确保已经进入对应目录

sql 复制代码
$ ll
total 16
drwxrwxr-x 3 user user 4096 Nov 14 12:00 ./
drwxrwxr-x 4 user user 4096 Nov 14 12:00 ../
-rw-rw-r-- 1 user user  179 Nov 14 12:00 Cargo.toml
drwxrwxr-x 2 user user 4096 Nov 14 12:00 src/
$ cargo build
   Compiling hello_rust v0.1.0 (~/hello_rust)
    Finished dev [unoptimized + debuginfo] target(s) in 0.28s

如果用 cargo 启动编译时当前位置不在工程目录下,cargo 会返回错误,比如当前位置在工程目录的上一级

bash 复制代码
$ ll
total 16
drwxrwxr-x 4 fi fi 4096 Nov 14 14:37 ./
drwxrwxr-x 7 fi fi 4096 Nov 13 09:05 ../
drwxrwxr-x 4 fi fi 4096 Nov 14 14:37 hello_rust/
$ cargo build
error: could not find `Cargo.toml` in `.` or any parent directory

成功编译完成后,看看 cargo 对工程目录做了哪些动作

python 复制代码
$ tree .
.
├── Cargo.lock
├── Cargo.toml
├── src
│   └── main.rs
└── target
    ├── CACHEDIR.TAG
    └── debug
        ├── build
        ├── deps
        │   ├── hello_rust-60af8441ea417b4f
        │   └── hello_rust-60af8441ea417b4f.d
        ├── examples
        ├── hello_rust
        ├── hello_rust.d
        └── incremental
            └── hello_rust-2bjlx50128sgs
                ├── s-gqkvfbv0nw-nhv7s5-2a8o7auiovgp3nlm4z796zmid
                │   ├── 1qtaaaupissaafq6.o
                │   ├── 2qka353ans86m1vf.o
                │   ├── 2zldp55fybgg4sw7.o
                │   ├── 411rdzeep3a70sxa.o
                │   ├── 519um16fwej7v8y2.o
                │   ├── dep-graph.bin
                │   ├── nq9mqbleo31lrwv.o
                │   ├── query-cache.bin
                │   └── work-products.bin
                └── s-gqkvfbv0nw-nhv7s5.lock

9 directories, 18 files

可以看到 cargo 管理的编译过程产生了大量的中间文件,并存放在工程的 target 子目录下,与工程同名的目标输出文件 hello_rust 也在子目录 target/debug 中。

执行输出文件

从上面可以看到 cargo 启动工程编译后的输出文件存放在工程子目录 target/debug 中,按理可以根据路径手动执行

shell 复制代码
$ ./target/debug/hello_rust 
Hello, world!

但上面的输入看起来略显麻烦,所以 cargo 提供了 run 指令直接执行 package 的输出文件

scss 复制代码
$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.04s
     Running `target/debug/hello_rust`
Hello, world!

无论是从路径调用还是用 cargo run 调用,执行结果一致。

工程开发过程中,我们需要不断修改代码,然后重新输出,那么怎么中途清除那些中间文件呢?除了手动删除文件和文件夹之外,cargo 也提供了 clean 指令方便清理编译中间文件

css 复制代码
$ cargo clean
$ tree .
.
├── Cargo.lock
├── Cargo.toml
└── src
    └── main.rs

1 directory, 3 files

为了演示,我这里稍微改动一下源文件 main.rs

arduino 复制代码
$ cat ./src/main.rs 
fn main() {
    println!("Hello rust's world!");
}

然后编译的时候重复依次输入 cargo build 和 cargo run 吗?

scss 复制代码
$ cargo build
   Compiling hello_rust v0.1.0 (~/hello_rust)
    Finished dev [unoptimized + debuginfo] target(s) in 0.46s
$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/hello_rust`
Hello rust's world!

其实,cargo run 会自动识别是否需要重新编译工程,然后再执行软件,所以如果你需要同时编译和执行软件,那么直接输入 cargo run 即可

scss 复制代码
$ cargo clean
$ tree .
.
├── Cargo.lock
├── Cargo.toml
└── src
    └── main.rs

1 directory, 3 files
$ cargo run
   Compiling hello_rust v0.1.0 (~/hello_rust)
    Finished dev [unoptimized + debuginfo] target(s) in 0.24s
     Running `target/debug/hello_rust`
Hello rust's world!
相关推荐
LvManBa1 天前
Vue学习记录之六(组件实战及BEM框架了解)
vue.js·学习·rust
VinciYan1 天前
Rust使用Actix-web和SeaORM库开发WebAPI通过Swagger UI查看接口文档
rust·api·web·orm
白总Server1 天前
MongoDB解说
开发语言·数据库·后端·mongodb·golang·rust·php
新知图书2 天前
Rust编程的作用域与所有权
开发语言·后端·rust
许野平2 天前
Rust: Warp RESTful API 如何得到客户端IP?
tcp/ip·rust·restful·ip地址
许野平2 天前
Rust:Result 和 Error
开发语言·后端·rust·error·result
Freestyle Coding2 天前
使用rust自制操作系统内核
c语言·汇编·microsoft·rust·操作系统
许野平3 天前
Rust 编译器使用的 C++ 编译器吗?
c++·rust
怪我冷i3 天前
Rust GUI框架Tauri V1 入门
rust
新知图书3 天前
Rust的常量
算法·机器学习·rust