文章目录
- 关于 Cargo
- 构建项目
- 创建工程
- 编译运行
- build
- clean
- 管理依赖
- 添加依赖
- update
- check
- 计时
- manual
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, seecargo -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(二)