Cargo - 构建 rust项目、管理依赖包

embedded/2024/9/23 22:06:41/

在这里插入图片描述

文章目录


rust 安装可参考:https://blog.csdn.net/lovechris00/article/details/124808034


关于 Cargo

  • Cargo 官方文档 : https://doc.rust-lang.org/cargo/
  • crates : https://crates.io

Cargo is the Rust package manager.
Cargo downloads your Rust package’s dependencies, compiles your packages, makes distributable packages, and uploads them to crates.io, the Rust community’s package registry.


构建项目

创建工程

cargo new world_hello

目录结构如下

$ tree
.
├── Cargo.toml
└── src└── main.rs1 directory, 2 files

  • Cargo.toml 为 Rust 的清单文件。其中包含了项目的元数据和依赖库。
  • src/main.rs 为编写应用代码的地方。

编译运行

cargo run

   Compiling world_hello v0.1.0 (/Users/user/Documents/code/scode1/rust/world_hello)Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.95sRunning `target/debug/world_hello`
Hello, world!

会产生编译文件

$ tree -L 3
.
├── Cargo.lock
├── Cargo.toml
├── src
│   └── main.rs
└── target├── CACHEDIR.TAG└── debug├── build├── deps├── examples├── incremental├── world_hello└── world_hello.d

运行 build/run 命令会创建一个新文件 Cargo.lock,该文件记录了本地所用依赖库的精确版本。


build

使用 run 就会自动build,但有时候我们可以只使用 build

cargo build
  • 会生成 debug 文件夹:world_hello/target/debug
  • 将 debug 文件夹删掉,再次 build 会产生新的 debug 文件夹;
  • 如果原文件没修改,build 后,debug 的内容可能不会更新

clean

cargo clean

debug 等文件夹会被删掉

$ cargo cleanRemoved 119 files, 11.4MiB total
$ tree
.
├── Cargo.lock
├── Cargo.toml
└── src└── main.rs1 directory, 3 files

管理依赖

创建项目会自动生成 Cargo.toml 文件
原始内容如下:

[package]
name = "world_hello"
version = "0.1.0"
edition = "2021"[dependencies]

添加依赖

在 Rust 中,我们通常把包称作crates
你可以在 crates 库搜索依赖:https://crates.io/crates/rand

这里以添加 rand 为例,添加在 [dependencies] 下方

...
[dependencies]
rand = "0.3.14"

再次 build

$ cargo buildUpdating crates.io indexDownloaded rand v0.3.23Downloaded rand v0.4.6Downloaded libc v0.2.154Downloaded 3 crates (831.0 KB) in 1.22sCompiling libc v0.2.154Compiling rand v0.4.6Compiling rand v0.3.23Compiling world_hello v0.1.0 (/Users/user/Documents/code/scode1/rust/world_hello)Finished `dev` profile [unoptimized + debuginfo] target(s) in 4.11s

查看文件结构
多出了 rand 相关内容

$ tree
.
├── Cargo.lock
├── Cargo.toml
├── src
│   └── main.rs
└── target├── CACHEDIR.TAG└── debug├── build│   ├── libc-f94129358965b880│   │   ├── invoked.timestamp│   │   ├── out│   │   ├── output│   │   ├── root-output│   │   └── stderr│   └── libc-fc8c71922db79826│       ├── build-script-build│       ├── build_script_build-fc8c71922db79826│       └── build_script_build-fc8c71922db79826.d├── deps│   ├── libc-2cd8d736a65e3bdc.d│   ├── libc-2cd8d736a65e3bdc.libc.844209738d3bf612-cgu.0.rcgu.o│   ├── liblibc-2cd8d736a65e3bdc.rlib│   ├── liblibc-2cd8d736a65e3bdc.rmeta│   ├── librand-51e82a279537c372.rlib│   ├── librand-51e82a279537c372.rmeta│   ├── librand-76148f2644fb6b76.rlib│   ├── librand-76148f2644fb6b76.rmeta│   ├── rand-51e82a279537c372.d│   ├── ...│   ├── rand-76148f2644fb6b76.rand.46a886c6f1f6cb04-cgu.4.rcgu.o│   ├── world_hello-7dfffed2f60728f0│   ├── ...│   └── world_hello-ffc760ff065d95f4.rvrclgiszz772pw.rcgu.o├── examples├── incremental│   ├── world_hello-2gr5fivx8ev9t│   │   ├── s-gvxy0ymbb9-azi538-5zea3fzl7hz9mcsbpmuo9lnu2│   │   │   ├── 2ao7q67wh7pwb6v2.o│   │   │   ├── ...│   │   │   └── work-products.bin│   │   └── s-gvxy0ymbb9-azi538.lock│   └── world_hello-32qpckyi3s6et│       ├── s-gvxxyb5ewl-1g1nr9k-5ewriqusvpjsrgth3731ddhf3│       │   ├── 2jcl4b6uedw0auwo.o│       │   ├── ...│       │   ├── rvrclgiszz772pw.o│       │   └── work-products.bin│       └── s-gvxxyb5ewl-1g1nr9k.lock├── world_hello└── world_hello.d14 directories, 65 files

update

更新所有依赖

cargo update

更新指定库

cargo update -p rand

check

查看对目录进行了哪些更改

修改内容 按 倒序排序

运行 check 之前,cargo clean 清理目录

$ cargo checkChecking libc v0.2.154Checking rand v0.4.6Checking rand v0.3.23Checking world_hello v0.1.0 (/Users/user/Documents/code/scode1/rust/world_hello)Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.94s

计时

查看编译时间

time cargo build

manual

cargo help

Rust’s package manager

Usage: cargo [+toolchain] [OPTIONS] [COMMAND]
cargo [+toolchain] [OPTIONS] -Zscript <MANIFEST_RS> [ARGS]…


Options:

  • -V, --version, Print version info and exit
    • -list, List installed commands
    • -explain <CODE, Provide a detailed explanation of a rustc error message
  • -v, --verbose... , Use verbose output (-vv very verbose/build.rs output)
  • -q, --quiet, Do not print cargo log messages
    • -color <WHEN, Coloring: auto, always, never
  • -C <DIRECTORY, Change to DIRECTORY before doing anything
    , , (nightly-only)
    • -frozen, Require Cargo.lock and cache are up to date
    • -locked, Require Cargo.lock is up to date
    • -offline, Run without accessing the network
    • -config <KEY=VALUE, Override a configuration value
  • -Z <FLAG>, Unstable (nightly-only) flags to Cargo, see cargo -Z help for details
  • -h, --help, Print help

Commands:

  • build, b : Compile the current package
  • check, c : Analyze the current package and report errors, but don’t build object files
  • clean: Remove the target directory
  • doc, d, Build this package’s and its dependencies’ documentation
  • new, : Create a new cargo package
  • init: Create a new cargo package in an existing directory
  • add, : Add dependencies to a manifest file
  • remove, Remove dependencies from a manifest file
  • run, r, Run a binary or example of the local package
  • test, t : Run the tests
  • bench: Run the benchmarks
  • update, Update dependencies listed in Cargo.lock
  • search, Search registry for crates
  • publish : Package and upload this package to the registry
  • install : Install a Rust binary
  • uninstall Uninstall a Rust binary
  • … : See all commands with --list

See cargo help <command> for more information on a specific command.


写完以上后,发现这个官方教程就挺好,参考:
<https://www.rust-lang.org/zh-CN/learn >


伊织 2024-05-07(二)


http://www.ppmy.cn/embedded/37017.html

相关文章

Docker 容器日志占用空间过大解决办法

1、vi /etc/docker/daemon.json {"log-driver":"json-file","log-opts": {"max-size":"200m", "max-file":"1"} } 2、重新加载守护进程配置文件 systemctl daemon-reload 3、重启docker systemctl…

Qt作业2

1、思维导图 2、练习&#xff1a;优化登录框&#xff0c;输入完用户名和密码后&#xff0c;点击登录&#xff0c;判断账户是否为 Admin 密码 为123456&#xff0c;如果判断成功&#xff0c;则输出登录成功&#xff0c;并关闭整个登录界面&#xff0c;如果登录失败&#xff0c;则…

Java面试题:解释volatile关键字的作用,以及它如何保证内存的可见性

在编程中&#xff0c;特别是在并发编程和多线程环境中&#xff0c;volatile 关键字是一个用于声明变量的特殊关键字&#xff0c;它主要有以下几个作用&#xff1a; 可见性&#xff1a;volatile 保证了在一个线程中对一个变量的修改对于其他线程是可见的。也就是说&#xff0c;当…

【数学】矩阵与矩阵乘法

矩阵 定义一个 n m n\times m nm 的矩阵如下&#xff1a; [ a 1 , 1 ⋯ a 1 , m ⋮ ⋱ ⋮ a n , 1 ⋯ a n , m ] \begin{bmatrix}a_{1,1}&\cdots&a_{1,m}\\\vdots&\ddots&\vdots\\a_{n,1}&\cdots&a_{n,m}\end{bmatrix} ​a1,1​⋮an,1​​⋯⋱⋯​…

Unity射击游戏开发教程:(10)创建主界面

主界面开发 玩游戏时,主菜单是事后才想到要做的。实际上几乎每个游戏都有一个主界面。如果你点击打开游戏并立即开始游戏,你会感到非常惊讶。本文将讨论如何创建带有启动新游戏的交互式按钮的主界面/主菜单。 主菜单将是一个全新的场景。我们将添加一个 UI 图像元素,并在图像…

Spring Task 定时任务没有定时执行是为什么?

目录 SpringBoot 定时任务的原理任务一直阻塞会怎么样&#xff1f;多个定时任务的执行具有相同表达式的定时任务&#xff0c;它们的执行顺序如何&#xff1f;Spring Task 和 Linux crontab 的 cron 语法区别&#xff1f;在 cron 语法中容易犯的错误Async 异步注解原理及作用并发…

JavaScript异步编程——03-Ajax传输json和XML

Ajax 传输 JSON JSON 的语法 JSON(JavaScript Object Notation)&#xff1a;是 ECMAScript 的子集。作用是进行数据的交换。语法更为简洁&#xff0c;网络传输、机器解析都更为迅速。 语法规则&#xff1a; 数据在键值对中 数据由逗号分隔 花括号保存对象 方括号保存数组…

算法精讲:选择排序

基本思想 每一趟从待排序的数据元素中选出最小(或最大)的一个元素,顺序放在待排序的数列的最前端,直到所有元素排完。 排序过程 初始关键字:【3 1 5 4 2】 第一趟排序后:1【3 5 4 2】 第二趟排序后:1 2【5 4 3】 第三趟排序后:1 2 3【4 5】 最终结果:1 2 3 4 5…