Rust:cargo的常用命令

1.查看版本

$ cargo --version

cargo 1.79.0 (ffa9cf99a 2024-06-03)

2.创建新的项目

$ cargo new hello

创建后的目录结构为

$ tree hello/

hello/

├── Cargo.toml

└── src

└── main.rs

3.运行项目

$ cd hello

$ cargo run

Compiling hello v0.1.0 (/home/chenkun/hello)

Finished `dev` profile unoptimized + debuginfo target(s) in 0.35s

Running `target/debug/hello`

Hello, world!

即可完成编译及运行项目

4.编译项目

$ cd hello

$ cargo build

编译后的可执行文件位于:hello/target/debug/hello

5.仅检查部编译

有的时候修改完程序可能需要检查一下程序是否可编译,而不需要真正的编译,以加快检查效率,可以通过

$ cargo check

6.编译发布版程序

当程序通过了各种测试后,可以通过下面的命令编译经过优化后运行的更快的发布版

$ cargo build --release

7.更新依赖库

当第一次运行cargo build时,cargo会根据项目目录中的文件Cargo.toml中描述的依赖表块dependencies下的内容,基于当前的依赖描述,构建一个Cargo.lock文件,该文件将锁定本项目的依赖,以保存项目构建的稳定性,不至于每次编译都在使用不同版本的依赖。

如果需要在依赖描述的范围内升级依赖包的小版本,可以通过以下命令:

$ cargo update

相关推荐
SomeOtherTime14 小时前
第 11 讲(大结局):编译期元编程与零拷贝架构——卫生声明宏(macro_rules!)、过程宏 AST 手术刀与 Serde 零反射序列化的终极合龙
rust
SomeOtherTime19 小时前
番外篇(第 12 讲):走向工业界深水区——从物理定律看 Rust 在真实工程中的“降维打击”与“阿喀琉斯之踵”
开发语言·rust
Kapaseker20 小时前
Rust 桌面开发到底怎么选?先看这篇文章!
rust
奶油话梅糖21 小时前
容器启停 2.5 毫秒:11 天 1,700 Star 的 Kiri,用一个 900 KB 的 PID 1 干掉了 runc 和 dockerd
前端·人工智能·rust
wflynn1 天前
GitHub 今日推荐|kiri:clone3 系统调用直达容器,2.5 毫秒启动无 runc 中间层
docker·rust·kubernetes·开源·github·containers
SomeOtherTime2 天前
第 4 讲:突破静态枷锁——智能指针(Box/Rc/Arc)与内部可变性(Cell/RefCell/UnsafeCell)的内存暗道
rust
SomeOtherTime2 天前
第 9 讲:异步并发与 Pin 的底层解剖——async/await 无栈协程状态机脱糖、自引用死锁的终极救赎与 Future/Waker 驱动模型
rust
wflynn2 天前
GitHub 今日推荐|Stela:在 PC 上直接启动 macOS 的翻译层
rust·开源·github
aifannihao3 天前
Rust 1.100 的 DropGuard:用 Python 实测失败回滚与吞异常
python·rust·异常处理·标准库·contextlib
鶴哥只手遮天3 天前
《rustc 内幕》第 1 章 全景:rustc 编译流水线与 crate 版图
后端·rust·开源