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

相关推荐
geovindu18 小时前
rust: Timing Functions Pattern
开发语言·后端·设计模式·rust·计时函数模式·性能分析型模式
关键词安全21 小时前
PHP / Node.js / Go / Rust 全部中招(CVE-2026-109618、109619、111497、111943)完整排查与修复清单
golang·rust·node.js
xjz18421 天前
Rust 的`+=`、`-=`、`*=`、`/=` —— 让变量更新更简洁
rust
xjz18422 天前
Rust 用 `&&`、`||`、`!` 把条件组合起来
rust
不积跬步无以至千里mmbb263 天前
Rust三大核心:Trait Crate Mod详解
linux·rust
SomeOtherTime3 天前
第 2 讲:借用检查器(Borrow Checker)的数学本质——读写锁的编译期化、非词法生命周期(NLL)与重借用(Reborrowing)
rust
xjz18424 天前
Rust 的else:把「做不做」升级成「二选一」
rust
喵个咪4 天前
RushWind Admin — update_mask 写面:字段级白名单、显式清除与点路径合并
后端·rust
喵个咪4 天前
RushWind Admin — 多租户行级隔离与租户回收站:Viewer 三态、repo_shell! 与全域级联清除
后端·rust
喵个咪4 天前
RushWind CMS — 三服务拓扑:core 领域层、双 BFF 薄代理与一份契约的双面输出
后端·rust