cargo项目构建和包管理工具(配置cargo国内源)、Cargo.toml配置项参数说明、cargo run运行项目
文章目录
- 一、Cargo
- 1. 配置cargo国内源
- 2. cargo来进行项目构建
- 3. cargo run运行项目
- 二、Cargo.toml配置项参数说明
- 1. 工作常用配置参数
- [[bin]] 二进制目标
- Cargo.toml中的[workspace]members起什么作用?
- Cargo工作空间
- [lib]
- Rust 中的 bin, lib, rlib, a, so 概念介绍
一、Cargo
官网: https://doc.rust-lang.org/cargo/index.html
cargo,简单来说就是python 的pip,nodejs 的npm,rust下的包管理工具。
Cargo用于组织Rust项目,比直接用rustc编译多个源文件更方便。
Rust 由 rustup 工具来安装和管理。 Rust 有一个 6 周的 快速发布过程 并且支持 大量的平台 ,所以任何时候都有很多 Rust 构建可用。 rustup 在 Rust 支持的每一个平台上以一致的方式管理这些构建, 并可以从 beta 和 nightly 发布渠道安装 Rust,且支持额外的交叉编译目标平台。
cargo new project_name --bin # 如果你想写一个普通的项目
cargo new lib_name --lib --vcs none # 如果你想写一个库
cargo build # 如果你想编译,默认会编译到target/debug/project_name下
cargo run # 如果你想编译并运行
cargo build --release # 如果你想发布,这会做很多优化,并编译到target/release/project_name下
cargo update # 如果你想修改Cargo.lock文件的话,运行它
cargo update -p rand # 如果你只是想更新rand版本的话,运行它
cargo test abc # 如果你想做test,运行它
1. 配置cargo国内源
原文链接:https://blog.csdn.net/u010953692/article/details/106464851
rust cargo指定国内镜像
参考URL: https://blog.csdn.net/setlilei/article/details/106204105?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase
/root/.cargo新建配置文件config
[source.crates-io]
replace-with = 'tuna'[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
cargo install cargo-rls-install
cargo rls-install -i nightly-2020-03-19
rustc -V
2. cargo来进行项目构建
执行
cargo new hello_cargo --bin
,执行完上面的操作之后,我们切换到hell_cargo目录下,可以看到一个文件(Cargo.toml)和一个目录(src),同时src目录下有一个main.rs文件。执行 cargo run 就可以看到一行"hello world" 字符串出现在屏幕上
3. cargo run运行项目
可以使用 cargo run 在一个命令中同时编译并运行生成的可执行文件。当然,也可以使用 cargo run --release 运行发布版本。
demo:
cargo run -p aptos-node --release -- -f /opt/aptos/public_full_node.yaml
当我们通过cargo run运行程序时,会调用target目录下面的可执行程序。
-p: 指定运行target中哪个包
–release: 优化编译,编译时间会变长
–: 双虚线将参数与cargo分开,明确指定参数-f 是传递给应用程序。
因此如果需要将参数传递给应用程序,而不是cargo,需要使用两个虚线将cargo run与参数分开。
[root@dev release]# cargo run -h
cargo-run
Run a binary or example of the local packageUSAGE:cargo run [OPTIONS] [--] [args]...ARGS:<args>...OPTIONS:-q, --quiet Do not print cargo log messages--bin [<NAME>] Name of the bin target to run--example [<NAME>] Name of the example target to run-p, --package [<SPEC>...] Package with the target to run-v, --verbose Use verbose output (-vv very verbose/build.rs output)-j, --jobs <N> Number of parallel jobs, defaults to # of CPUs--color <WHEN> Coloring: auto, always, never-r, --release Build artifacts in release mode, with optimizations--frozen Require Cargo.lock and cache are up to date--profile <PROFILE-NAME> Build artifacts with the specified profile--features <FEATURES> Space or comma separated list of features to activate--locked Require Cargo.lock is up to date--all-features Activate all available features--offline Run without accessing the network--config <KEY=VALUE> Override a configuration value (unstable)--no-default-features Do not activate the `default` feature--target <TRIPLE> Build for the target triple-Z <FLAG> Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' fordetails--target-dir <DIRECTORY> Directory for all generated artifacts--manifest-path <PATH> Path to Cargo.toml--message-format <FMT> Error format--unit-graph Output build graph in JSON (unstable)--ignore-rust-version Ignore `rust-version` specification in packages--timings[=<FMTS>...] Timing output formats (unstable) (comma separated): html, json-h, --help Print help informationRun `cargo help run` for more detailed information.
[root@dev release]# cargo run --release -h
cargo-run
Run a binary or example of the local packageUSAGE:cargo run [OPTIONS] [--] [args]...ARGS:<args>...OPTIONS:-q, --quiet Do not print cargo log messages--bin [<NAME>] Name of the bin target to run--example [<NAME>] Name of the example target to run-p, --package [<SPEC>...] Package with the target to run-v, --verbose Use verbose output (-vv very verbose/build.rs output)-j, --jobs <N> Number of parallel jobs, defaults to # of CPUs--color <WHEN> Coloring: auto, always, never-r, --release Build artifacts in release mode, with optimizations--frozen Require Cargo.lock and cache are up to date--profile <PROFILE-NAME> Build artifacts with the specified profile--features <FEATURES> Space or comma separated list of features to activate--locked Require Cargo.lock is up to date--all-features Activate all available features--offline Run without accessing the network--config <KEY=VALUE> Override a configuration value (unstable)--no-default-features Do not activate the `default` feature--target <TRIPLE> Build for the target triple-Z <FLAG> Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' fordetails--target-dir <DIRECTORY> Directory for all generated artifacts--manifest-path <PATH> Path to Cargo.toml--message-format <FMT> Error format--unit-graph Output build graph in JSON (unstable)--ignore-rust-version Ignore `rust-version` specification in packages--timings[=<FMTS>...] Timing output formats (unstable) (comma separated): html, json-h, --help Print help informationRun `cargo help run` for more detailed information.
[root@dev release]#
二、Cargo.toml配置项参数说明
https://doc.rust-lang.org/cargo/reference/index.html#cargo-reference
1. 工作常用配置参数
[[bin]] 二进制目标
二进制目标是可执行程序,可以在编译后运行。 默认二进制文件名是src / main.rs,默认为包的名称。 其他二进制文件存储在SRC / BIN /目录中。 每个二进制文件的设置可以在Cargo.Toml中的[[Bin]]表中定制。
您可以使用带有 --bin <bin-name>
的cargo run
命令运行单个二进制文件。 cargo install
安装可用于将可执行文件复制到common 位置。
# Example of customizing binaries in Cargo.toml.
[[bin]]
name = "cool-tool"
test = false
bench = false[[bin]]
name = "frobnicator"
required-features = ["frobnicate"]
Cargo.toml中的[workspace]members起什么作用?
members相当于你自己可以在src中添加其它的二进制package,然后可以引用这些二进制package里的东西;
dependencies则是针对于library package
Cargo工作空间
这是为了开发大型程序,分治crate用的。
一,根cargo.toml内容
[workspace]
members = [
“adder”,
“add-one”,
]
二,adder里的cargo.toml内容
[package]
name = “adder”
version = “0.1.0”
authors = [“test test@qq.com”]
edition = “2018”
[dependencies]
add-one = { path = “…/add-one” }
三,main.rs内容
use add_one;
fn main() {
let num = 10;
println!(“Hello, world! {} plus one is {}!”, num, add_one::add_one(num));
}
四,Lib.rs内容
pub fn add_one(x: i32) -> i32 {
x + 1
}
#[cfg(test)]
mod tests {
use super:
cargo项目构建和包管理工具(配置cargo国内源)、Cargo.toml配置项参数说明、cargo run运行项目相关推荐
- Python 包管理工具poetry配置国内PyPI镜像源
不论是使用 pip,还是Pipenv.Poetry等工具,安装Python包的时候会默认从官方的PyPI源下载文件,速度比较慢.国内的一些公司和机构提供了 PyPI 镜像源(mirror source ...
- Helm包管理工具(简介、安装、方法)
认识Helm 每次我们要部署一个应用都需要写一个配置清单(维护一套yaml文件),但是每个环境又不一样.部署一套新的环境成本是真的很高.如果我们能够使用类似于yum的工具来安装我们的应用的话那就太好了 ...
- Day04-NPM包管理工具
NPM包管理工具 一.简介 1.什么是NPM 2.NPM工具的安装位置 二.使用npm管理项目 1.创建文件夹npmdemo 2.项目初始化 3.修改npm镜像 4.npm install命令的使用, ...
- Go:包管理工具GOPATH、vendor、dep 、go module
目录 Go包管理工具:前言 GOPATH vendor.dep Go modules Module 文件 go mod命令 Go modules使用步骤: go module的文件下载后位置: Go包 ...
- golang中的包管理工具——govendor和godep简单学习
为什么用vendor目录 依赖问题 我们知道,一个工程稍大一点,通常会依赖各种各样的包.而Go使用统一的GOPATH管理依赖包,且每个包仅保留一个版本.而不同的依赖包由各自的版本工具独立管理,所以当所 ...
- 新一代的 Python 包管理工具 -- PDM
PDM 是一个新的 Python 的包管理器,也许你还未知晓它的存在,但实际上PDM 已经诞生两年,并在 2021 年发布 1.0 版本,目前最高的版本是 1.12.8. 在刚听到 PDM 时,我下意 ...
- 前端工程化-包管理工具npm-yarn-cnpm-pnpm详细介绍以及如何选择
文章目录 包管理工具详解 npm包管理工具 1.代码共享的方案 2.npm包管理工具介绍 3.npm的配置文件 常见配置文件 常见配置文件属性 版本号的说明 4.npm install npm ins ...
- 现代化程序开发笔记(4)——包管理工具
本系列文章以我的个人博客的搭建为线索(GitHub 仓库:Evian-Zhang/evian-blog),记录我在现代化程序设计中的一些笔记.在这篇文章中,我会就项目构建工具和包管理工具做一些讨论,先 ...
- Nodejs入门 (四) 包管理工具(npm、cnpm、yarn、nvm)
目录 一.什么是包管理工具? 二.npm的使用 1.初始化 2.搜索包 3.下载安装包 4.安装包的使用 5.全局安装 6.安装包依赖 7.安装指定版本的包 8.删除依赖 9.配置命令别名 10.np ...
最新文章
- Samba共享后不能访问,或者看不到文件,是selinux惹的祸
- Python运算符和编码
- Java 字符串操作的总结1(转载)
- 用十条命令在一分钟内检查Linux服务器性能
- leetcode869. 重新排序得到 2 的幂
- Android无线测试之—UiAutomator UiDevice API介绍六
- python自顶向下设计步骤_python自底向上的执行单元测试
- linux 进程崩溃log,linux调试:dmesg 查看程序崩溃原因分析方法之一
- 原生js 实现购物车价格和总价 统计
- HTML 标题h1-h6
- html转pdf手机,html转pdf
- appcrash事件怎么解决?三种方法教你
- 淘宝关键词搜索商品接口分析商品价格走势(商品列表接口,商品销量接口,商品价格接口,分类ID采集商品数据接口)接口代码对接教程
- 03 graphx 从 SSSP 来看 pregel
- 《自控力》第四章读书笔记
- H2O with R 简明使用手记·下篇
- NET性能优化-推荐使用Collections.Pooled(补充)
- 揭秘一个不起眼的微商新品牌,如何快速赢得客户信任?
- ATA学习记录(1)download microcode
- 电力预测|基于新型MDPSO-SVR混合模型的电力预测特征选择(Matlab代码实现)
热门文章
- 数据仓库(四)之ETL开发
- html 绘制甘特图,基于JS简单甘特图
- 基于python的智能文本分析 书_Python文本分析
- 论文EI号查找相关事宜
- 一、大白话谈搞好交互
- Jordan标准型的由来?为何n阶数字方阵都必有对应的Jordan标准型?怎么求可逆矩阵P?
- hadoop高可用hdfs搭建(三节点)
- java注解教程 pdf_Java注解教程和示例
- 2017CCPC秦皇岛 M:Safest Buildings
- 3D操纵杆[机械手臂式鼠标(6轴关节鼠标/6自由度鼠标)]