当前位置: 首页 > news >正文

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

在这里插入图片描述

文章目录

    • 关于 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, 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(二)

相关文章:

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

文章目录 关于 Cargo构建项目创建工程编译运行buildclean 管理依赖添加依赖updatecheck计时 manual rust 安装可参考&#xff1a;https://blog.csdn.net/lovechris00/article/details/124808034 关于 Cargo Cargo 官方文档 &#xff1a; https://doc.rust-lang.org/cargo/crat…...

内网安全-代理Socks协议路由不出网后渗透通讯CS-MSF控制上线简单总结

我这里只记录原理&#xff0c;具体操作看文章后半段或者这篇文章内网渗透—代理Socks协议、路由不出网、后渗透通讯、CS-MSF控制上线_内网渗透 代理-CSDN博客 注意这里是解决后渗透通讯问题&#xff0c;之后怎么提权&#xff0c;控制后面再说 背景 只有win7有网&#xff0c;其…...

NSSCTF | [SWPUCTF 2021 新生赛]jicao

打开题目&#xff0c;发现高亮显示了一个 php 脚本 这是脚本的内容 <?php highlight_file(index.php); include("flag.php"); $id$_POST[id]; $jsonjson_decode($_GET[json],true); if ($id"wllmNB"&&$json[x]"wllm") {echo $flag;…...

Redis 支持的 Java 客户端都有哪些?

Redis 是一种高性能的键值存储系统&#xff0c;它以其快速、灵活和可扩展的特性而闻名。在 Java 开发中&#xff0c;与 Redis 交互的方式通常是通过使用 Redis 的 Java 客户端。 这些客户端提供了访问 Redis 数据库的接口&#xff0c;使开发人员能够在 Java 应用程序中轻松地使…...

【JavaEE 初阶(四)】多线程进阶

❣博主主页: 33的博客❣ ▶️文章专栏分类:JavaEE◀️ &#x1f69a;我的代码仓库: 33的代码仓库&#x1f69a; &#x1faf5;&#x1faf5;&#x1faf5;关注我带你了解更多线程知识 目录 1.前言2.常见的锁策略2.1悲观锁vs乐观锁2.2轻量级锁vs重量级锁2.3自旋锁vs挂起锁2.4读写…...

ZOC8 for Mac v8.08.1激活版:卓越性能的SSH客户端

在远程连接和管理的世界中&#xff0c;ZOC8 for Mac以其卓越的性能和丰富的功能&#xff0c;成为了众多专业人士的首选SSH客户端。它支持SSH1、SSH2、Telnet、Rlogin、Serial等多种协议&#xff0c;让您轻松连接到远程服务器。ZOC8拥有简洁直观的界面和强大的功能设置&#xff…...

指针(4)有点难

指针&#xff08;4&#xff09; 来做个简单的回顾&#xff1a; 指针数组&#xff1a; 1.是数组 2.是存放指针的数组 char* arr1[5]; int*arr2[3]; 数组指针&#xff1a; 1 .是指针 2 .指向数组的指针 字符指针&#xff1a;char*pc; 整型指针&#xff1a;int*pi; int …...

初步了解json文件

来自wetab 的AI pro: JSON&#xff08;JavaScript Object Notation&#xff09;是一种轻量级的数据交换格式&#xff0c;易于人阅读和编写&#xff0c;同时也易于机器解析和生成。JSON采用完全独立于语言的文本格式&#xff0c;但是它使用了类似于编程语言&#xff08;特别是J…...

赶紧收藏!2024 年最常见 100道 Java 基础面试题(四十)

上一篇地址&#xff1a;赶紧收藏&#xff01;2024 年最常见 100道 Java 基础面试题&#xff08;三十九&#xff09;-CSDN博客 七十九、forward和redirect的区别&#xff1f; 在Java Web应用程序中&#xff0c;forward和redirect是两种不同的服务器端重定向机制&#xff0c;它…...

初步了解Kubernetes

目录 1. K8S概述 1.1 K8S是什么 1.2 作用 1.3 由来 1.4 含义 1.5 相关网站 2. 为什么要用K8S 3. K8S解决的问题 4. K8S的特性 5. Kubernetes集群架构与组件 6. 核心组件 6.1 Master组件 6.1.1 Kube-apiserver 6.1.2 Kube-controller-manager 6.1.3 kube-schedul…...

前端工程化的基本介绍

文章目录 一、概念二、前端工程化的细节模块化组件化规范化 一、概念 工程化&#xff0c;可以理解为使用一些方式&#xff0c;去改良提高行业中现有的步骤、设计、应用方式。前端工程化&#xff0c;就是指对前端进行一些流程的标准化&#xff0c;让开发变得更有效率&#xff0…...

linux上Redis安装使用

环境centOS8 redis是缓存数据库&#xff0c;主要是用于在内存中存储数据&#xff0c;内存的读写很快&#xff0c;加快系统读写数据库的速度 一、Linux 安装 Redis 1. 下载Redis 官网下载Downloads - Redis 历史版本Index of /releases/ 本文中安装的版本为&#xff1a;h…...

prometheus+grafana的安装与部署及优点

一、Prometheus 的优点 1、非常少的外部依赖&#xff0c;安装使用超简单&#xff1b; 2、已经有非常多的系统集成 例如&#xff1a;docker HAProxy Nginx JMX等等&#xff1b; 3、服务自动化发现&#xff1b; 4、直接集成到代码&#xff1b; 5、设计思想是按照分布式、微服…...

JWK和JWT 学习

JWK和JWT 介绍 JWK (JSON Web Key) 和 JWT (JSON Web Token) 是现代Web应用程序中用于安全通信的两个重要概念。它们都是基于JSON的&#xff0c;并且是OAuth 2.0和OpenID Connect等协议的核心组成部分。 官方文档 JWT官方网站 JWK和JWK Set的RFC文档 JWT的RFC文档 JWK (JS…...

Go 使用mqtt

1、创建一个文件夹&#xff0c;并且使用go modules go mod init <module_name> 其中<module_name>是你的模块名称&#xff0c;如下 go mod init example.com/myproject 2、安装mqtt扩展 go get github.com/eclipse/paho.mqtt.golang 3、开始写主程序 package ma…...

C++ primer plus习题及解析第十二章(类和动态内存分配)

题目&#xff1a;12.1 题&#xff1a; 对于下面的类声明&#xff1a; class Cow { private:char name[20];char* hobby;double weight; public:Cow();Cow(const char* nm, const char* ho, double wt);//有参构造Cow(const Cow& c);//拷贝构造函数~Cow();//析构函数Cow&…...

gdb调试功能描述

gdb调试功能描述 gdb 调试&#xff1a;只对可执行文件进行调用&#xff0c;无法直接用gdb调试.c文件 1.查找命令帮助&#xff1a; &#xff08;gdb&#xff09; help data &#xff08;gdb&#xff09; help call -l (list) 查看载入文件&#xff08;默认为10行&#xff09…...

使用Simulink Test进行单元测试

本文摘要&#xff1a;主要介绍如何利用Simulink Test工具箱&#xff0c;对模型进行单元测试。内容包括&#xff0c;如何创建Test Harness模型&#xff0c;如何自动生成excel格式的测试用例模板来创建测试用例&#xff0c;如何手动填写excel格式的测试用例模板来手动创建测试用例…...

深度学习中超参数设置

1、batchsize 在训练深度学习模型时&#xff0c;batch size&#xff08;批大小&#xff09;和 epochs&#xff08;迭代次数&#xff09;之间的关系取决于您的数据集大小、模型复杂度、计算资源等因素。下面是一些一般性的指导原则&#xff1a; 较大的 Batch Size&#xff1a;通…...

Docker nsenter 命令使用

查看容器对应宿主机上面的pid&#xff0c;容器技术的实质是进程&#xff0c;并没有完整的操作系统&#xff0c;就相当于在主机上面fork了一个子进程&#xff0c;通过docker daemon去fork一个子进程&#xff0c;这个子进程是可以在主机上面看到其pid的。 $ docker inspect -f {…...

React Native 开发环境搭建(全平台详解)

React Native 开发环境搭建&#xff08;全平台详解&#xff09; 在开始使用 React Native 开发移动应用之前&#xff0c;正确设置开发环境是至关重要的一步。本文将为你提供一份全面的指南&#xff0c;涵盖 macOS 和 Windows 平台的配置步骤&#xff0c;如何在 Android 和 iOS…...

PHP和Node.js哪个更爽?

先说结论&#xff0c;rust完胜。 php&#xff1a;laravel&#xff0c;swoole&#xff0c;webman&#xff0c;最开始在苏宁的时候写了几年php&#xff0c;当时觉得php真的是世界上最好的语言&#xff0c;因为当初活在舒适圈里&#xff0c;不愿意跳出来&#xff0c;就好比当初活在…...

Cilium动手实验室: 精通之旅---20.Isovalent Enterprise for Cilium: Zero Trust Visibility

Cilium动手实验室: 精通之旅---20.Isovalent Enterprise for Cilium: Zero Trust Visibility 1. 实验室环境1.1 实验室环境1.2 小测试 2. The Endor System2.1 部署应用2.2 检查现有策略 3. Cilium 策略实体3.1 创建 allow-all 网络策略3.2 在 Hubble CLI 中验证网络策略源3.3 …...

江苏艾立泰跨国资源接力:废料变黄金的绿色供应链革命

在华东塑料包装行业面临限塑令深度调整的背景下&#xff0c;江苏艾立泰以一场跨国资源接力的创新实践&#xff0c;重新定义了绿色供应链的边界。 跨国回收网络&#xff1a;废料变黄金的全球棋局 艾立泰在欧洲、东南亚建立再生塑料回收点&#xff0c;将海外废弃包装箱通过标准…...

C# 类和继承(抽象类)

抽象类 抽象类是指设计为被继承的类。抽象类只能被用作其他类的基类。 不能创建抽象类的实例。抽象类使用abstract修饰符声明。 抽象类可以包含抽象成员或普通的非抽象成员。抽象类的成员可以是抽象成员和普通带 实现的成员的任意组合。抽象类自己可以派生自另一个抽象类。例…...

【论文阅读28】-CNN-BiLSTM-Attention-(2024)

本文把滑坡位移序列拆开、筛优质因子&#xff0c;再用 CNN-BiLSTM-Attention 来动态预测每个子序列&#xff0c;最后重构出总位移&#xff0c;预测效果超越传统模型。 文章目录 1 引言2 方法2.1 位移时间序列加性模型2.2 变分模态分解 (VMD) 具体步骤2.3.1 样本熵&#xff08;S…...

docker 部署发现spring.profiles.active 问题

报错&#xff1a; org.springframework.boot.context.config.InvalidConfigDataPropertyException: Property spring.profiles.active imported from location class path resource [application-test.yml] is invalid in a profile specific resource [origin: class path re…...

深度学习水论文:mamba+图像增强

&#x1f9c0;当前视觉领域对高效长序列建模需求激增&#xff0c;对Mamba图像增强这方向的研究自然也逐渐火热。原因在于其高效长程建模&#xff0c;以及动态计算优势&#xff0c;在图像质量提升和细节恢复方面有难以替代的作用。 &#x1f9c0;因此短时间内&#xff0c;就有不…...

【Nginx】使用 Nginx+Lua 实现基于 IP 的访问频率限制

使用 NginxLua 实现基于 IP 的访问频率限制 在高并发场景下&#xff0c;限制某个 IP 的访问频率是非常重要的&#xff0c;可以有效防止恶意攻击或错误配置导致的服务宕机。以下是一个详细的实现方案&#xff0c;使用 Nginx 和 Lua 脚本结合 Redis 来实现基于 IP 的访问频率限制…...

Python Einops库:深度学习中的张量操作革命

Einops&#xff08;爱因斯坦操作库&#xff09;就像给张量操作戴上了一副"语义眼镜"——让你用人类能理解的方式告诉计算机如何操作多维数组。这个基于爱因斯坦求和约定的库&#xff0c;用类似自然语言的表达式替代了晦涩的API调用&#xff0c;彻底改变了深度学习工程…...