当前位置:网站首页>通过 Cargo 管理 Rust 项目
通过 Cargo 管理 Rust 项目
2022-06-27 17:58:00 【用户3147702】
1. 引言
经过前面几篇文章的介绍,我们已经清楚了 rust 的基础语法:
随着我们开发的程序越来越复杂,项目中的合作越来越频繁时,就必须要考虑如何来组织我们的项目。
rust 官方提供了 Cargo 工具来管理项目,让我们的项目管理变得简单,本文我们就来介绍一下。
2. 使用 cargo 创建项目
2.1 创建项目
在 rust 工具包中,已经默认安装了 cargo,你可以通过下面命令来检验 cargo 是否已经正常安装,并且查看其版本:
$ cargo --version使用 cargo 创建项目很简单,只需要以项目名为参数执行命令:
$ cargo new hello_cargo这样就创建出了名为 hello_cargo 的项目。
进入 hello_cargo 目录执行 tree 命令可以看到:
.
├── Cargo.toml
└── src
└── main.rs
1 directory, 2 files2.2 Cargo.toml
从新创建的 hello_cargo 项目中的内容来看,除了我们熟悉的写有 hello world 程序的 main.rs 外,其余就只有一个 Cargo.toml 文件了。
Cargo.toml 文件是一个 TOML 标准格式文件,它初始的内容如下:
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]我们需要依赖的第三方包以及其版本就可以定义在这个配置文件中,在 build 的时候自动安装这些依赖。
在 rust 中,包是对术语 crate 的翻译,它是 Rust 最小的编译单元,package 是若干 crate 的集合,在中文中,crate 和 package 都被称为“包”,有时我们需要去辨别包与包的不同,不过大部分情况并不需要。
2.3 代码版本控制
Cargo 默认使用 git 作为版本控制工具,因此上述项目目录中已经有了 .gitignore 文件,当然,在创建项目时,也可以通过 --vcs 参数来选择不使用任何版本控制工具,或者通过 --help 查看支持的其他版本控制工具参数。
3. 用 Cargo 构建和运行项目
在项目目录下,执行 cargo build 命令就可以完成任务的构建:
$ cargo build
Compiling hello_cargo v0.1.0 (/Users/techlog/Workspace/code/rust/hello_cargo)
Finished dev [unoptimized + debuginfo] target(s) in 2.01s通过 tree 命令,可以看到构建完成后产生的新文件:
.
├── Cargo.lock
├── Cargo.toml
├── src
│ └── main.rs
└── target
├── CACHEDIR.TAG
└── debug
├── build
├── deps
│ ├── hello_cargo-961ecc8ba604608e
│ ├── hello_cargo-961ecc8ba604608e.1qau2g7rc04b8n42.rcgu.o
│ ├── hello_cargo-961ecc8ba604608e.1wb2m83aihx8d8k4.rcgu.o
│ ├── hello_cargo-961ecc8ba604608e.2cxjpttsw6fohl8z.rcgu.o
│ ├── hello_cargo-961ecc8ba604608e.2ff6ogfujp34yrx2.rcgu.o
│ ├── hello_cargo-961ecc8ba604608e.48wqmd6nlep2ungd.rcgu.o
│ ├── hello_cargo-961ecc8ba604608e.54gbqr1pabl9mrem.rcgu.o
│ ├── hello_cargo-961ecc8ba604608e.5edo85wf9vpk0c0.rcgu.o
│ ├── hello_cargo-961ecc8ba604608e.cfznvsvgjwaw8ji.rcgu.o
│ └── hello_cargo-961ecc8ba604608e.d
├── examples
├── hello_cargo
├── hello_cargo.d
└── incremental
└── hello_cargo-cj2sb71suuq
├── s-gakbxnyar6-xajrwz-fg4gunogpa2i
│ ├── 1qau2g7rc04b8n42.o
│ ├── 1wb2m83aihx8d8k4.o
│ ├── 2cxjpttsw6fohl8z.o
│ ├── 2ff6ogfujp34yrx2.o
│ ├── 48wqmd6nlep2ungd.o
│ ├── 54gbqr1pabl9mrem.o
│ ├── 5edo85wf9vpk0c0.o
│ ├── cfznvsvgjwaw8ji.o
│ ├── dep-graph.bin
│ ├── query-cache.bin
│ └── work-products.bin
└── s-gakbxnyar6-xajrwz.lock我们关心的实际上是 target/debug/hello_cargo 这个可执行文件,可以直接执行它:
$ ./target/debug/hello_cargo
Hello, world!除了这些中间文件,以及最后的可执行文件外,在项目目录下还出现了 Cargo.lock 文件,它记录了构建当前项目所依赖的库和版本号。
如果你要编译正式发布版本,那么在构建的时候增加 --release 参数即可。
4. 检测 rust 语法
当你做了一些修改,你可能并不想花很长时间来编译,而仅仅是想要知道是否存在编译错误,此时,只需要执行 cargo check 就可以快速定位编译错误:
$ cargo check
Compiling hello_cargo v0.1.0 (file:///projects/hello_cargo)
Finished dev [unoptimized + debuginfo] target(s) in
0.32 secs5. 运行项目
执行 cargo run 命令可以运行项目,如果之前没有执行过 cargo build 或者在 build 后又有了新的修改,cargo run 命令会自动进行 build:
$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.06s
Running `target/debug/hello_cargo`
Hello, world!边栏推荐
猜你喜欢

爬取国家法律法规数据库

GIS遥感R语言学习看这里

Solution of adding st-link to Huada MCU Keil

Introduction to deep learning and neural networks

Blink SQL内置函数大全

Running lantern experiment based on stm32f103zet6 library function

Embracing cloud Nativity: Practice of Jiangsu Mobile order center

GIS remote sensing R language learning see here

Doctoral Dissertation of the University of Toronto - training efficiency and robustness in deep learning

Jinyuan's high-end IPO was terminated: it was planned to raise 750million Rushan assets and Liyang industrial investment were shareholders
随机推荐
A simple calculation method of vanishing point
labelimg使用指南
crontab的学习随笔
SQL Server - Window Function - 解决连续N条记录过滤问题
Garbage collector driving everything -- G1
New Zhongda chongci scientific and Technological Innovation Board: annual revenue of 284million and proposed fund-raising of 557million
Function key input experiment based on stm32f103zet6 Library
1027 Colors in Mars
UE4-Actor基础知识
通过 G1 GC Log 重新认识 G1 垃圾回收器
Erreur Keil de Huada Single Chip Computer La solution de Weak
Leetcode 1381. 设计一个支持增量操作的栈
MySQL表的增删改查(基础)
【登录界面】
Pyhton爬取百度文库文字写入word文档
MySQL初学者福利
2022年第一季度消费金融APP用户洞察——总数达4479万人
ABAP-SM30删除前检查
基于STM32F103ZET6库函数按键输入实验
过关斩将,擒“指针”(下)