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

相关推荐
SomeOtherTime16 小时前
Rust 闭包语法和原理(AI回答)
开发语言·后端·rust
SomeOtherTime20 小时前
Rust集合(AI回答)
开发语言·后端·rust
SomeOtherTime1 天前
Rust 泛型与特性讲解(AI回答)
rust
青川Lapis2 天前
RUST:错误处理 thiserror + anyhow 工程化
rust
SomeOtherTime2 天前
Rust生命周期介绍(AI回答)
开发语言·后端·rust
SomeOtherTime2 天前
Rust所有权(AI回答)
开发语言·后端·rust
SomeOtherTime3 天前
Rust 面向对象介绍(AI回答)
开发语言·python·rust
西风测评4 天前
安全的自动更新怎么做:minisign 签名、先验签后解析与防回滚
rust·ssh
Delightrs4 天前
Rust LeetCode238 算法题解
开发语言·算法·rust
奶油话梅糖4 天前
快过 IDM 5 倍、开源免费:4,200 Star 的 Rust 下载器用 7 组对照实验“单挑“行业老大
rust·开源·idm