Cargo - 管理 rust 依赖包

news/2024/9/23 22:06:39/

在这里插入图片描述

文章目录


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/news/1456644.html

相关文章

JavaScript内置对象列举

Object对象&#xff1a; Object是JavaScript中最基本的对象&#xff0c;它有一些常用的属性和方法。示例代码&#xff1a;let person {name: John,age: 30,occupation: engineer };console.log(Object.keys(person)); // 输出对象的所有属性名 [name, age, occupation] conso…

Java基础学习:Dozer映射框架

文章目录 一、Dozer 介绍二、Dozer的API映射方式三、使用1、java程序2、springboot项目&#xff08;1&#xff09;引入依赖&#xff08;2&#xff09;声明工具类&#xff08;3&#xff09;工具类委托spring管理 一、Dozer 介绍 Dozer是一个Java工具&#xff0c;主要用于在相同…

文字转语音粤语怎么转换?6个软件教你快速进行文字转换语音

文字转语音粤语怎么转换&#xff1f;6个软件教你快速进行文字转换语音 当需要将文字转换为粤语语音时&#xff0c;可以使用多种工具和服务&#xff0c;这些工具可以帮助您快速而准确地实现这一目标。以下是六个非国内的语音转换软件&#xff0c;它们可以帮助您将文字转换为粤语…

SpringBoot中实现发送邮件

概要 在Spring Boot中发送电子邮件相对简单。你可以使用Spring的邮件支持来实现这一点。 步骤&#xff1a; 1.添加依赖&#xff1a;首先&#xff0c;需要在你的pom.xml文件中添加Spring Boot的邮件发送器依赖。 2. 配置邮件服务器&#xff1a;在application.properties或app…

C#知识|将选中的账号信息展示到控制台(小示例)

哈喽&#xff0c;你好啊&#xff0c;我是雷工&#xff01; 上篇学习了控件事件的统一关联&#xff0c; 本篇通过实例练习继续学习事件统一处理中Tag数据获取、对象的封装及泛型集合List的综合运用。 01 实现功能 在上篇的基础上实现&#xff0c;点击选中喜欢的账号&#xff0…

Java解决垂直鉴权问题(对垂直权限进行校验)

Java解决垂直鉴权问题&#xff08;对垂直权限进行校验&#xff09; 文章目录 Java解决垂直鉴权问题&#xff08;对垂直权限进行校验&#xff09;前言一、垂直鉴权是什么&#xff1f;二、实现过程1.新建接口权限菜单映射表2.项目初始化时加载接口菜单映射关系3.自定义过滤器拦截…

Elasticsearch中对文章进行索引和查重

解决思路 要在Elasticsearch中对文章进行索引和查重&#xff0c;可以按照以下步骤操作&#xff1a; 安装Elasticsearch并启动服务。 安装Python的Elasticsearch客户端库&#xff0c;可以使用pip install elasticsearch命令进行安装。 编写Python代码&#xff0c;使用Elastic…

我独自升级崛起在哪下载 我独自升级崛起客户端下载教程

定于5月8日全球盛放的《我独自升级&#xff1a;崛起》——这一激动人心的动作角色扮演游戏巨作&#xff0c;汲取了同名动漫及网络漫画的精髓&#xff0c;誓将以其无与伦比的魅力&#xff0c;引领玩家迈入一个探索深远、规模宏大的奇幻之旅。游戏构筑在一个独一无二的网络武侠世…