【RUST学习日记】第2课 Cargo

简介: 【RUST学习日记】第2课 Cargo

0x00 回顾


上一节,咱们了解了Rust,学会了编译hello world。这节课咱们来了解下Cargo构建工具。 


0x01 认识Cargo


现在介绍另外一个Rust构建工具和包管理器。安装完成Rust环境之后,Cargo自然也就附带安装了。其实在实际的项目开发过程中,建议咱们都用Cargo来管理项目,方便维护。

首先先介绍下Cargo 的常用命令:


image.png


0x02 Cargo常用命令


新建项目

cargo new + 项目名称 :创建一个项目

咱们找一个空文件夹,执行cargo new hello_rust


0a2653c851af460fa595bd959398a8f1.png


咱们打开创建的项目,看下目录结构


2d65d23f6d4748949b924e4057485923.png


其目录结构,多出了一个Cargo.toml文件,那么这个文件是用来做什么的呢?咱们待会儿再介绍。

├─hello_rust
 │    ├─Cargo.toml
 │    ├─src
 │    │    ├─main.rs

src/main.rs文件中,命令给咱们自动生成了hello world的代码。


编译项目


需要进入Rust项目目录,执行cargo build命令。咱们刚刚创建了hello_rust项目,因此咱们先执行cd hello_rust进入hello_rust目录,再执行cargo build命令。


6de278e6d6694ce5bb08e7e842b7e74b.png


命令执行结束后,咱们的项目目录,多出了target目录,当前的目录结构如下:

├─hello_rust
 │    ├─Cargo.lock
 │    ├─Cargo.toml
 │    ├─src
 │    │    ├─main.rs
 │    ├─target
 │    │    ├─.rustc_info.json
 │    │    ├─CACHEDIR.TAG
 │    │    ├─debug
 │    │    │    ├─.cargo-lock
 │    │    │    ├─.fingerprint
 │    │    │    │    ├─hello_rust-48ba2e065b74c352
 │    │    │    │    │    ├─bin-hello_rust
 │    │    │    │    │    ├─bin-hello_rust.json
 │    │    │    │    │    ├─dep-bin-hello_rust
 │    │    │    │    │    ├─invoked.timestamp
 │    │    │    ├─build
 │    │    │    ├─deps
 │    │    │    │    ├─hello_rust.d
 │    │    │    │    ├─hello_rust.exe
 │    │    │    │    ├─hello_rust.pdb
 │    │    │    ├─examples
 │    │    │    ├─hello_rust.d
 │    │    │    ├─hello_rust.exe
 │    │    │    ├─hello_rust.pdb
 │    │    │    ├─incremental
 │    │    │    │    ├─hello_rust-2uvfimco9bni1
 │    │    │    │    │    ├─s-fzaa2j599b-kngfhq-2b2uk2xznk0bm
 │    │    │    │    │    │    ├─16ohm4271lofwczj.o
 │    │    │    │    │    │    ├─1sx9bynqce00nts9.o
 │    │    │    │    │    │    ├─2b2ua0pllx29qutz.o
 │    │    │    │    │    │    ├─3eeqpquxpjwl9x0b.o
 │    │    │    │    │    │    ├─4sazo1qpoqeg0t30.o
 │    │    │    │    │    │    ├─5k8ackmjiop1ilj.o
 │    │    │    │    │    │    ├─dep-graph.bin
 │    │    │    │    │    │    ├─query-cache.bin
 │    │    │    │    │    │    ├─work-products.bin
 │    │    │    │    │    ├─s-fzaa2j599b-kngfhq.lock

target/debug目录下有个hello_rust.exe,没错这就是咱们最终编译好的文件,这跟咱们使用rustc命令编译的结果是一致的,打开它就可以运行了。哈哈,是不是一闪而过,原因上节已经解释过了。


PS:项目准备发布时,可以执行cargo build -release进行优化编译项目,运行这条命令后则会在target/release目录生成一个可执行文件。


编译运行项目


需要进入Rust项目目录,执行cargo run命令。咱们刚刚执行了,cargo build,现在咱们执行cargon run(如下图所示),执行时间0.06s,运行结果也打印了出来,运行的程序则是咱们上面所说的在target/debug目录下有个hello_rust.exe


8ec4f2997fb246878c34ecd6d122b7c6.png


编译后运行


其实,cargo run这条命令的功能是编译运行,如果项目没有编译,它则会先帮咱们编译,然后再运行结果。咱们这次做个测试,将目录下的target文件夹删除,再次执行cargo run


12c3b7f3f8814309a195c64f051d4445.png


在执行过程中,很明显感到会慢一些,此次执行时间是2.75s,从而得出一个结论:在创建项目后,执行过cargo build后再执行cargo run则会跳过编译过程,直接运行程序结果;否则,cargo run会先执行编译过程,再运行程序


创建文档


需要进入Rust项目目录,执行cargo doc命令。咱们尝试执行一下,然后看下项目的目录结构。

├─hello_rust
 │    ├─Cargo.lock
 │    ├─Cargo.toml
 │    ├─src
 │    │    ├─main.rs
 │    ├─target
 │    │    ├─.rustc_info.json
 │    │    ├─CACHEDIR.TAG
 │    │    ├─debug
 │    │    │    ├─.cargo-lock
 │    │    │    ├─.fingerprint
 │    │    │    │    ├─hello_rust-48ba2e065b74c352
 │    │    │    │    │    ├─bin-hello_rust
 │    │    │    │    │    ├─bin-hello_rust.json
 │    │    │    │    │    ├─dep-bin-hello_rust
 │    │    │    │    │    ├─doc-bin-hello_rust
 │    │    │    │    │    ├─doc-bin-hello_rust.json
 │    │    │    │    │    ├─invoked.timestamp
 │    │    │    ├─build
 │    │    │    ├─deps
 │    │    │    │    ├─hello_rust.d
 │    │    │    │    ├─hello_rust.exe
 │    │    │    │    ├─hello_rust.pdb
 │    │    │    ├─examples
 │    │    │    ├─hello_rust.d
 │    │    │    ├─hello_rust.exe
 │    │    │    ├─hello_rust.pdb
 │    │    │    ├─incremental
 │    │    │    │    ├─hello_rust-2uvfimco9bni1
 │    │    │    │    │    ├─s-fzalohxenn-ikyc86-2b2uk2xznk0bm
 │    │    │    │    │    │    ├─16ohm4271lofwczj.o
 │    │    │    │    │    │    ├─1sx9bynqce00nts9.o
 │    │    │    │    │    │    ├─2b2ua0pllx29qutz.o
 │    │    │    │    │    │    ├─3eeqpquxpjwl9x0b.o
 │    │    │    │    │    │    ├─4sazo1qpoqeg0t30.o
 │    │    │    │    │    │    ├─5k8ackmjiop1ilj.o
 │    │    │    │    │    │    ├─dep-graph.bin
 │    │    │    │    │    │    ├─query-cache.bin
 │    │    │    │    │    │    ├─work-products.bin
 │    │    │    │    │    ├─s-fzalohxenn-ikyc86.lock
 │    │    ├─doc
 │    │    │    ├─.lock
 │    │    │    ├─brush.svg
 │    │    │    ├─COPYRIGHT.txt
 │    │    │    ├─dark.css
 │    │    │    ├─down-arrow.svg
 │    │    │    ├─favicon.ico
 │    │    │    ├─FiraSans-LICENSE.txt
 │    │    │    ├─FiraSans-Medium.woff
 │    │    │    ├─FiraSans-Regular.woff
 │    │    │    ├─hello_rust
 │    │    │    │    ├─all.html
 │    │    │    │    ├─fn.main.html
 │    │    │    │    ├─index.html
 │    │    │    │    ├─sidebar-items.js
 │    │    │    ├─LICENSE-APACHE.txt
 │    │    │    ├─LICENSE-MIT.txt
 │    │    │    ├─light.css
 │    │    │    ├─main.js
 │    │    │    ├─normalize.css
 │    │    │    ├─noscript.css
 │    │    │    ├─rust-logo.png
 │    │    │    ├─rustdoc.css
 │    │    │    ├─search-index.js
 │    │    │    ├─settings.css
 │    │    │    ├─settings.html
 │    │    │    ├─settings.js
 │    │    │    ├─source-files.js
 │    │    │    ├─source-script.js
 │    │    │    ├─SourceCodePro-LICENSE.txt
 │    │    │    ├─SourceCodePro-Regular.woff
 │    │    │    ├─SourceCodePro-Semibold.woff
 │    │    │    ├─SourceSerifPro-Bold.ttf.woff
 │    │    │    ├─SourceSerifPro-It.ttf.woff
 │    │    │    ├─SourceSerifPro-LICENSE.md
 │    │    │    ├─SourceSerifPro-Regular.ttf.woff
 │    │    │    ├─src
 │    │    │    │    ├─hello_rust
 │    │    │    │    │    ├─main.rs.html
 │    │    │    ├─storage.js
 │    │    │    ├─theme.js
 │    │    │    ├─wheel.svg

项目目录多出了doc目录,咱们打开doc/─hello_rust/index.html。没错,自动生成了文档。说起写文档应该是咱们最头痛的事情了。下图中红框标注的是项目目录,蓝框则是咱们项目中的方法。


34e8d716411043c08c7ffba9fbba23de.png


但是main方法没有任何注释,我在main.rs中随便加一点注释。

PS:关于“文档注释”,会在后面的章节详细介绍,这里了解即可。

/// 我是文档注释,下面的方法会打印 “hello world”。
fn main() {
    println!("Hello, world!");
}

咱们再次执行cargo doc命令,打开doc/─hello_rust/index.html,看下结果。页面中已经增加了咱们刚刚写的注释内容了。

PS:写代码时多写注释,可以方便其它人维护代码哟~


92ba0822ed0b46e1ae72df8a17d3a45b.png


测试项目


需要进入Rust项目目录,执行cargo test命令。这条指令很简单,测试下在咱们写的程序。test result :ok。


d79b274929334152a6d38be91e2d1be3.png

 

0x03 toml文件


toml是rust的配置文件。咱们常见的配置文件有xmljsonyaml。在Rust中,则使用了toml作为该语言的配置文件,可以配置依赖的库,项目信息等等。咱们用记事本可以打开看下里面的内容。


dfc80ca9d8004e6c9ddc00e8448ffc6a.png


关于toml我就不多介绍了,文末我会推荐几篇文章,具体想了解的可以看下。下节预告——如何选择IDE?


0x04 其它资料


  • toml官方Github (https://github.com/toml-lang/toml)
相关文章
|
4月前
|
Rust 区块链
学Rust不学Cargo,等于没学Rust:features特性详解
在 Rust 中,Cargo 的 "features" 是一种条件编译机制,允许在编译 crate 时编译部分代码。这样可以在一个 crate 中提供多个功能,并根据需要选择性地启用或禁用这些功能。
141 1
|
2天前
|
存储 Rust 编译器
【Rust学习】07_结构体说明
**struct**或 ***structure***是一种自定义数据类型,允许您命名和包装多个相关的值,从而形成一个有意义的组合。如果您熟悉面向对象的语言,那么**struct**就像对象中的数据属性。在本章中,我们将比较和对比元组与结构体,在您已经知道的基础上,来演示结构体是对数据进行分组的更好方法。
11 1
|
14天前
|
Rust 编译器 开发者
Cargo:Rust的神秘助手,它将如何改变包管理游戏规则?
【8月更文挑战第31天】Rust的包管理器Cargo简化了依赖管理和构建过程,与编译器无缝集成,提供从依赖下载到编译构建的全套解决方案。通过`cargo new`创建项目后,编辑`Cargo.toml`文件即可轻松管理依赖。Cargo还支持自动生成文档、运行测试及发布代码,并通过`crates.io`平台方便查找和分享Rust库,是Rust生态系统中的重要工具,有助于提升开发者生产力。
35 1
|
12天前
|
Rust Linux Go
Rust/Go语言学习
Rust/Go语言学习
|
1月前
|
存储 Rust 安全
【Rust学习】06_切片
所有权、借用和切片的概念确保了 Rust 程序在编译时的内存安全。Rust 语言提供了跟其他系统编程语言相同的方式来控制你使用的内存,但拥有数据所有者在离开作用域后自动清除其数据的功能意味着你无须额外编写和调试相关的控制代码。
18 1
|
2月前
|
Rust 测试技术 编译器
Rust与C++的区别及使用问题之Rust项目中组织目录结构的问题如何解决
Rust与C++的区别及使用问题之Rust项目中组织目录结构的问题如何解决
|
2月前
|
存储 Rust 安全
【Rust学习】04_所有权
所有权是 Rust 最独特的特性,对语言的其余部分有着深远的影响。它使 Rust 能够在不需要垃圾收集器的情况下保证内存安全,因此了解所有权的运作方式非常重要。在本章中,我们将讨论所有权以及几个相关功能:借用、切片以及 Rust 如何在内存中布局数据。
18 1
|
2月前
|
存储 Rust 编译器
【Rust学习】03_通用编程概念
您成功了!这是一个相当大的章节:您了解了变量、标量和复合数据类型、函数、注释、 if 表达式和循环!若要练习本章中讨论的概念。
28 2
|
2月前
|
Rust 编译器
【Rust学习】05_引用和借用
在这章我们将开始学习Rust的引用和借用,它们是Rust中重要的概念,它们允许我们创建可变引用,以及创建不可变引用。
18 0
|
3月前
|
Rust Shell 索引
使用阿里云镜像加速Rust与Cargo安装及更新
使用阿里云镜像加速Rust与Cargo安装及更新
366 0