imtoken钱包网站|cargo
安装 - Cargo 手册 中文版
安装 - Cargo 手册 中文版
简介1. 入门1.1. 安装1.2. 初次使用 Cargo2. Cargo 指南2.1. 为什么存在 Cargo2.2. 创建一个新的包2.3. 处理现有包2.4. 依赖2.5. 项目布局2.6. Cargo.toml 和 Cargo.lock2.7. 测试2.8. 持续集成2.9. Cargo Home2.10. 构建缓存3. Cargo 参考3.1. 指定依赖3.1.1. Overriding Dependencies3.2. 清单格式3.2.1. Cargo Targets3.3. Workspaces3.4. Features3.4.1. Features Examples3.5. Profiles3.6. 设置3.7. 环境变量3.8. 构建脚本3.8.1. Build Script Examples3.9. 将 crate 发布到 Crates.io3.10. 包 ID 规格3.11. 更换源3.12. 外部工具3.13. Registries3.14. Dependency Resolution3.15. SemVer Compatibility3.16. 不稳定功能4. Cargo Commands4.1. General Commands4.1.1. cargo4.1.2. cargo help4.1.3. cargo version4.2. Build Commands4.2.1. cargo bench4.2.2. cargo build4.2.3. cargo check4.2.4. cargo clean4.2.5. cargo doc4.2.6. cargo fetch4.2.7. cargo fix4.2.8. cargo run4.2.9. cargo rustc4.2.10. cargo rustdoc4.2.11. cargo test4.3. Manifest Commands4.3.1. cargo generate-lockfile4.3.2. cargo locate-project4.3.3. cargo metadata4.3.4. cargo pkgid4.3.5. cargo tree4.3.6. cargo update4.3.7. cargo vendor4.3.8. cargo verify-project4.4. Package Commands4.4.1. cargo init4.4.2. cargo install4.4.3. cargo new4.4.4. cargo search4.4.5. cargo uninstall4.5. Publishing Commands4.5.1. cargo login4.5.2. cargo owner4.5.3. cargo package4.5.4. cargo publish4.5.5. cargo yank5. 参见问题6. 附录:术语白7. Appendix: Git Authentication
Light (default)
Rust
Coal
Navy
Ayu
Cargo 手册 中文版
安装
安装 Rust 和 Cargo
获得 Cargo 的最简单方法是使用rustup脚本,获取当前稳定版本的 Rust:
在 Linux 和 macOS 系统上,这可以通过以下方式完成:
$ curl -sSf https://static.rust-lang.org/rustup.sh | sh
它将下载一个脚本,然后开始安装。如果一切顺利,您会看到:
Rust is installed now. Great!
在 Windows 上,下载并运行rustup-init.exe。它将在控制台中启动安装,并在成功时显示上述消息.
在此之后,你可以使用rustup命令,安装beta或者nightly版本的 Rust 和 Cargo。
有关其他安装选项和信息,请访问 Rust 网站的安装页面.
从源头构建 Cargo
或者,你可以从源头构建 Cargo.
Cargo 教程 | 菜鸟教程
Cargo 教程 | 菜鸟教程
菜鸟教程 -- 学的不仅是技术,更是梦想!
首页
HTML
CSS
JavaScript
Vue
Bootstrap
NodeJS
Python3
Python2
Java
C
C++
C#
Go
SQL
Linux
jQuery
本地书签
首页
HTML
CSS
JS
本地书签
Search
Python3 教程
Python2 教程
Vue3 教程
vue2 教程
Bootstrap3 教程
Bootstrap4 教程
Bootstrap5 教程
Bootstrap2 教程
Rust 教程
Rust 教程
Rust 环境搭建
Cargo 教程
Rust 输出到命令行
Rust 基础语法
Rust 数据类型
Rust 注释
Rust 函数
Rust 条件语句
Rust 循环
Rust 所有权
Rust Slice(切片)类型
Rust 结构体
Rust 枚举类
Rust 组织管理
Rust 错误处理
Rust 泛型与特性
Rust 生命周期
Rust 文件与 IO
Rust 集合与字符串
Rust 面向对象
Rust 并发编程
Rust 宏
Rust 环境搭建
Rust 输出到命令行
Cargo 教程
Cargo 是什么
Cargo 是 Rust 的构建系统和包管理器。
Rust 开发者常用 Cargo 来管理 Rust 工程和获取工程所依赖的库。在上个教程中我们曾使用 cargo new greeting 命令创建了一个名为 greeting 的工程,Cargo 新建了一个名为 greeting 的文件夹并在里面部署了一个 Rust 工程最典型的文件结构。这个 greeting 文件夹就是工程本身。
Cargo 功能
Cargo 除了创建工程以外还具备构建(build)工程、运行(run)工程等一系列功能,构建和运行分别对应以下命令:
cargo build
cargo run
Cargo 还具有获取包、打包、高级构建等功能,详细使用方法参见 Cargo 命令。
在 VSCode 中配置 Rust 工程
Cargo 是一个不错的构建工具,如果使 VSCode 与它相配合那么 VSCode 将会是一个十分便捷的开发环境。
在上一章中我们建立了 greeting 工程,现在我们用 VSCode 打开 greeting 文件夹(注意不是 runoob-greeting)。打开 greeting 之后,在里面新建一个新的文件夹 .vscode (注意 vscode 前面的点,如果有这个文件夹就不需要新建了)。在新建的 .vscode 文件夹里新建两个文件 tasks.json 和 launch.json,文件内容如下:
tasks.json 文件
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command":"cargo",
"args": ["build"]
}
]
}
launch.json 文件(适用在 Windows 系统上)
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) 启动",
"preLaunchTask": "build",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false
},
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "这里填GDB所在的目录",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
launch.json 文件(适用在 Linux 系统上)
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug",
"type": "gdb",
"preLaunchTask": "build",
"request": "launch",
"target": "${workspaceFolder}/target/debug/${workspaceFolderBasename}",
"cwd": "${workspaceFolder}"
}
]
}
launch.json 文件(适用在 Mac OS 系统上)
{
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) 启动",
"type": "cppdbg",
"preLaunchTask": "build",
"request": "launch",
"program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
}
]
}
然后点击 VSCode 左栏的 "运行"。
如果你使用的是 MSVC 选择 "(Windows) 启动"。
如果使用的是 MinGW 且安装了 GDB 选择"(gdb)启动",gdb 启动前请注意填写 launch.json 中的 "miDebuggerPath"。
程序就会开始调试运行了。运行输出将出现在"调试控制台"中:
在 VSCode 中调试 Rust
调试程序的方法与其它环境相似,只需要在行号的左侧点击红点就可以设置断点,在运行中遇到断点会暂停,以供开发者监视实时变量的值。
Rust 环境搭建
Rust 输出到命令行
2 篇笔记
写笔记
#0 cargo expand pyl***@yandex.com 160再补充几个 cargo 的重要子命令:cargo clippy: 类似eslint,lint工具检查代码可以优化的地方cargo fmt: 类似go fmt,代码格式化cargo tree: 查看第三方库的版本和依赖关系cargo bench: 运行benchmark(基准测试,性能测试)cargo udeps(第三方): 检查项目中未使用的依赖另外 cargo build/run --release 使用 release 编译会比默认的 debug 编译性能提升 10 倍以上,但是 release 缺点是编译速度较慢,而且不会显示 panic backtrace 的具体行号cargo expand cargo expand pyl***@yandex.com3年前 (2020-10-13)
#0 apple@m1 app***m1.com 参考地址 43使用 M1 M2 芯片的苹果电脑, 还需要安装 CodeLLDB 插件, 并在 lunch.json 中指定 targetArchitecture 为 arm64:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "clang++ - Build and debug active file",
"type": "lldb",
"request": "launch",
"program": "./benchncnn",
"stopAtEntry": true,
"cwd": "/Users/oldpan/deps/ncnn/build_debug/benchmark/",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"targetArchitecture": "arm64"
}
]
}
apple@m1 apple@m1 app***m1.com 参考地址1年前 (2022-10-26)
点我分享笔记
取消
分享笔记
昵称昵称 (必填)
邮箱邮箱 (必填)
引用地址引用地址
分类导航
HTML / CSSHTML 教程HTML5 教程CSS 教程CSS3 教程Bootstrap3 教程Bootstrap4 教程Bootstrap5 教程Font Awesome 教程Foundation 教程 JavaScriptJavaScript 教程HTML DOM 教程jQuery 教程AngularJS 教程AngularJS2 教程Vue.js 教程Vue3 教程React 教程TypeScript 教程jQuery UI 教程jQuery EasyUI 教程Node.js 教程AJAX 教程JSON 教程Echarts 教程Chart.js 教程Highcharts 教程Google 地图 教程 服务端Python 教程Python2.x 教程Linux 教程Docker 教程Ruby 教程Java 教程C 教程C++ 教程Perl 教程Servlet 教程JSP 教程Lua 教程Rust 教程Scala 教程Go 教程PHP 教程数据结构与算法Django 教程FastAPI 教程Zookeeper 教程设计模式正则表达式Maven 教程Verilog 教程ASP 教程AppML 教程VBScript 教程 数据库SQL 教程MySQL 教程PostgreSQL 教程SQLite 教程MongoDB 教程Redis 教程Memcached 教程 数据分析Python 教程NumPy 教程Pandas 教程Matplotlib 教程Scipy 教程R 教程Julia 教程 移动端Android 教程Swift 教程jQuery Mobile 教程ionic 教程Kotlin 教程 XML 教程XML 教程DTD 教程XML DOM 教程XSLT 教程XPath 教程XQuery 教程XLink 教程XPointer 教程XML Schema 教程XSL-FO 教程SVG 教程 ASP.NETASP.NET 教程C# 教程Web Pages 教程Razor 教程MVC 教程Web Forms 教程 Web ServiceWeb Service 教程WSDL 教程SOAP 教程RSS 教程RDF 教程 开发工具Eclipse 教程Git 教程Svn 教程Markdown 教程 网站建设HTTP 教程网站建设指南浏览器信息网站主机教程TCP/IP 教程W3C 教程网站品质
Advertisement
反馈/建议
在线实例
·HTML 实例
·CSS 实例
·JavaScript 实例
·Ajax 实例
·jQuery 实例
·XML 实例
·Java 实例
字符集&工具
· HTML 字符集设置
· HTML ASCII 字符集
· JS 混淆/加密
· PNG/JPEG 图片压缩
· HTML 拾色器
· JSON 格式化工具
· 随机数生成器
最新更新
·
Rust 宏
·
Seaborn 教程
·
Pandas 相关性分析
·
31.2k star, 免...
·
Dev Home —...
·
免费开源的 AI ...
·
11.2k star, 免...
站点信息
·
意见反馈
·
免责声明
·
关于我们
·
文章归档
关注微信
Copyright © 2013-2024 菜鸟教程
runoob.com All Rights Reserved. 备案号:闽ICP备15012807号-1
微信关注
构建脚本 - Cargo 手册 中文版
构建脚本 - Cargo 手册 中文版
简介1. 入门1.1. 安装1.2. 初次使用 Cargo2. Cargo 指南2.1. 为什么存在 Cargo2.2. 创建一个新的包2.3. 处理现有包2.4. 依赖2.5. 项目布局2.6. Cargo.toml 和 Cargo.lock2.7. 测试2.8. 持续集成2.9. Cargo Home2.10. 构建缓存3. Cargo 参考3.1. 指定依赖3.1.1. Overriding Dependencies3.2. 清单格式3.2.1. Cargo Targets3.3. Workspaces3.4. Features3.4.1. Features Examples3.5. Profiles3.6. 设置3.7. 环境变量3.8. 构建脚本3.8.1. Build Script Examples3.9. 将 crate 发布到 Crates.io3.10. 包 ID 规格3.11. 更换源3.12. 外部工具3.13. Registries3.14. Dependency Resolution3.15. SemVer Compatibility3.16. 不稳定功能4. Cargo Commands4.1. General Commands4.1.1. cargo4.1.2. cargo help4.1.3. cargo version4.2. Build Commands4.2.1. cargo bench4.2.2. cargo build4.2.3. cargo check4.2.4. cargo clean4.2.5. cargo doc4.2.6. cargo fetch4.2.7. cargo fix4.2.8. cargo run4.2.9. cargo rustc4.2.10. cargo rustdoc4.2.11. cargo test4.3. Manifest Commands4.3.1. cargo generate-lockfile4.3.2. cargo locate-project4.3.3. cargo metadata4.3.4. cargo pkgid4.3.5. cargo tree4.3.6. cargo update4.3.7. cargo vendor4.3.8. cargo verify-project4.4. Package Commands4.4.1. cargo init4.4.2. cargo install4.4.3. cargo new4.4.4. cargo search4.4.5. cargo uninstall4.5. Publishing Commands4.5.1. cargo login4.5.2. cargo owner4.5.3. cargo package4.5.4. cargo publish4.5.5. cargo yank5. 参见问题6. 附录:术语白7. Appendix: Git Authentication
Light (default)
Rust
Coal
Navy
Ayu
Cargo 手册 中文版
Build Scripts
构建脚本
一些包需要编译第三方非 Rust 代码,例如 C 库。其他的包需要链接到 C 库,当然这些库既可以位于系统上,也可以从源代码构建。其他人或许还需要功能工具,比如构建之前的代码生成(想想解析生成器)。
Cargo 并不打算替换为这些能良好优化任务的其他工具,但是它与build配置选项.
[package]
# ...
build = "build.rs"
指定的build命令应执行的 Rust 文件(相对于包根),将在包编译其他内容之前,被编译和调用,从而具备 Rust 代码所依赖的构建或生成的工件。默认情况下 Cargo 在包根文件中寻找"build.rs"(即使您没有给build字段指定值)使用build = "custom_build_name.rs"指定自定义生成名,或build = false禁用对构建脚本的自动检测。
Build 命令的一些用例是:
构建一个捆绑的 C 库.
在主机系统上找到 C 库.
从规范中生成 Rust 模块.
为箱,执行所需的某平台特定配置.
下面将详细介绍每一个用例,以给出构建命令如何工作的示例.
Inputs to the Build Script
输入到构建脚本
当运行构建脚本时,存在许多构建脚本用到的输入,所有输入都以环境变量传入。
除了环境变量之外,构建脚本的当前目录是构建脚本包的源目录.
Outputs of the Build Script
构建脚本的输出
由构建脚本打印到 stdout 的所有行都被写入像target/debug/build/
# specially recognized by Cargo
cargo:rustc-link-lib=static=foo
cargo:rustc-link-search=native=/path/to/foo
cargo:rustc-cfg=foo
cargo:rustc-env=FOO=bar
# arbitrary user-defined metadata
cargo:root=/path/to/foo
cargo:libdir=/path/to/foo/lib
cargo:include=/path/to/foo/include
另一方面,打印到 stderr 的行被写入像target/debug/build/
Cargo 识别一些特殊的 key,其中一些影响箱的构造:
rustc-link-lib=[KIND=]NAME说明了,指定值是库名,且会作为-l标志传递给编译器。KIND可选为static,dylib(默认值),或framework的其中之一,用rustc --help见更多细节。
rustc-link-search=[KIND=]PATH说明了,指定值是库搜索路径,且会作为-L标志传递给编译器。KIND可选为dependency,crate,native,framework或all(默认值)的其中之一,使用rustc --help见更多细节.
rustc-flags=FLAGS是传递给编译器的一组标志,仅支持-l和-L标志。
rustc-cfg=FEATURE说明了,指定的特性,且会作为--cfg标志传递给编译器。这通常对检测,执行各种特征的编译时间,是有用的。
rustc-env=VAR=VALUE说明了,指定的环境变量,且会被添加到编译器所在的环境中。然后,可以通过编译箱中的env!宏检索该值。这对于在箱的代码中嵌入额外的元数据很有用,比如 Git HEAD 的散列,或持续集成服务器的唯一标识符。
rerun-if-changed=PATH是文件或目录的路径,说明了如果构建脚本发生更改(由文件上最近修改的时间戳检测到),则应重新运行构建脚本。通常,如果箱根目录中的任何文件发生更改,则重新运行构建脚本,但这可用于将更改范围扩展到仅一小组文件。(如果这个路径指向一个目录,则不会遍历整个目录以进行更改——只对目录本身的时间戳进行更改(该时间戳对应于目录中的某些类型的更改,取决于平台),将触发重新构建。要请求重新运行整个目录中的任何更改,请递归地为该目录打印一行,为该目录内的所有内容打印另一行。)请注意,如果构建脚本本身(或其依赖项之一)更改,则无条件地重新构建和重新运行该脚本,因此,cargo:rerun-if-changed=build.rs几乎总是冗余(除非您想要忽略除了build.rs,所有其他文件的变化)
rerun-if-env-changed=VAR是环境变量的名称,说明了它指示如果环境变量的值发生变化,则应重新运行构建脚本。这基本上与rerun-if-changed是一样的,除了它与环境变量一起工作。注意,这里的环境变量用于全局环境变量,如CC这样的,对于 Cargo 所设的像TARGET,就不必使用它。还要注意,如果rerun-if-env-changed打印出来,然后 Cargo 将只在,那些环境变量发生变化,或者打印出rerun-if-changed改变的文件的情况下,才重新运行构建脚本。
warning=MESSAGE是构建脚本运行完毕后,打印到主控制台的消息/警告只针对路径依赖项(即,您在本地工作的那些依赖项)显示,因此如, crates.io 的箱在默认情况下不会打印警告。
其他哪些元素都是用户定义的元数据,这些元数据传递给了依赖的。关于这个的更多信息可以在links部分查看.
Build Dependencies
构建依赖
构建脚本也可以依赖其他基于 Cargo 的箱。依赖关系通过清单的build-dependencies部分指定。
[build-dependencies]
foo = { git = "https://github.com/your-packages/foo" }
构建脚本不可以访问dependencies或dev-dependencies部分列表中的依赖项(它们还没有建成!),除非明确声明,否则包本身也不能使用所有构建依赖项。
The links Manifest Key
links 清单 键
除了清单键build,Cargo 也支持一个,要链接到本地库的名称声明,那就是links清单键:
[package]
# ...
links = "foo"
build = "build.rs"
此清单说明了包会链接到本机库libfoo,并且它还具有定位和/或构建该本机库的构建脚本。Cargo 要求build如果有值,那links也要有值。
这个清单键的目的是,让 Cargo 了解包所具有的本地依赖项集合,并提供在包构建脚本之间,传递元数据的合适的系统.
首先,Cargo 要求一个包最多只有一个links值。换句话说,禁止两个包链接到同一个本机库。然而,这里也有约定位置的方式,用来缓解这个问题。
如上面在输出格式中提到的,每个构建脚本可以以键-值对的形式生成一组任意的元数据。此元数据传递给依赖的包。例如,如果libbar依赖libfoo,当libfoo生成key=value作为其元数据的一部分,那libbar的构建脚本会有DEP_FOO_KEY=value环境变量。
注意,元数据只传递给直接依赖项,而不是把依赖项串起来。此元数据传递的动机,会在接下来,关联到系统库案例研究中概述。
Overriding Build Scripts
覆盖 构建脚本
如果一个清单包含links关键字,那 Cargo 支持重写用自定义库指定的构建脚本。此功能的目的是防止完全运行有问题的构建脚本,而是提前提供下元数据。
要覆盖构建脚本,请将下列配置放在任何可接受的 Cargo 的配置位置中。
[target.x86_64-unknown-linux-gnu.foo]
rustc-link-search = ["/path/to/foo"]
rustc-link-lib = ["foo"]
root = "/path/to/foo"
key = "value"
本节说明目标x86_64-unknown-linux-gnu,命名为foo的库,具有指定的元数据。此元数据与构建脚本时生成的元数据相同,提供了许多键/值对,其中rustc-flags,rustc-link-search和rustc-link-lib有点特殊.
使用此配置,如果一个包声明它链接到此foo,那构建脚本将不编译或运行,而会使用指定的元数据。
Case study: Code generation
案例学习: 代码生成
由于各种原因,一些 Cargo 包在编译之前需要生成代码。这里我们将介绍一个简单的示例,该示例把,'生成库调用'作为构建脚本的一部分.
首先,让我们看一下这个包的目录结构:
.
├── Cargo.toml
├── build.rs
└── src
└── main.rs
1 directory, 3 files
在这里我们可以看到我们有一个build.rs构建脚本,和二进制文件main.rs。 接下来,让我们看一下清单:
# Cargo.toml
[package]
name = "hello-from-generated-code"
version = "0.1.0"
authors = ["you@example.com"]
build = "build.rs"
在这里,我们可以看到,我们已经指定了一个构建脚本build.rs,我们将使用它来生成一些代码。让我们看看构建脚本里面有什么:
// build.rs
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;
fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("hello.rs");
let mut f = File::create(&dest_path).unwrap();
f.write_all(b"
pub fn message() -> &'static str {
\"Hello, World!\"
}
").unwrap();
}
这里有两点值得注意的地方:
脚本使用OUT_DIR环境变量,以知道输出文件到哪里。它可以使用进程的当前工作目录,来查找输入文件应该到哪里,但是在这种情况下,我们是没有任何输入文件的。
一般来说,构建脚本不应该修改OUT_DIR目录外的任何文件。 乍看之下,似乎不错,但当您使用这种箱子作为依赖项时,它确会带来问题,因为.cargo/registry源中的隐性的常量应该是不变的。cargo在打包时不会允许这样的脚本。
这个脚本相对简单,只是写出一个小生成的文件。可以想象,其他更奇特的操作也可能发生,例如从 C 头文件或其他定义的语言生成 Rust 模块。
接下来,我们来看看库本身:
// src/main.rs
include!(concat!(env!("OUT_DIR"), "/hello.rs"));
fn main() {
println!("{}", message());
}
这就是真正的魔法发生的地方。该库正在使用 rustc 定义的 include!宏,它又结合concat!与env!宏去包含生成文件(hello.rs),从而进入箱的编译。
使用此处所示的结构,箱可以包括(include)构建脚本在内的,任何数量的生成文件。
Case study: Building some native code
案例学习: 构建一些原生代码
有时需要建立一些本地 C 或 C++代码作为包的一部分。这是在用构建脚本到 Rust 箱本身之前,构建本机库的另一个极好用例。作为一个例子,我们将创建一个 Rust 库,它调用 C 来打印"Hello,World!".
和上面一样,让我们先来看看包的布局:
.
├── Cargo.toml
├── build.rs
└── src
├── hello.c
└── main.rs
1 directory, 4 files
很像之前的吧! 下一步,清单如下:
# Cargo.toml
[package]
name = "hello-world-from-c"
version = "0.1.0"
authors = ["you@example.com"]
build = "build.rs"
现在,我们不打算使用任何-构建的依赖项,所以现在让我们看一下构建脚本:
// build.rs
use std::process::Command;
use std::env;
use std::path::Path;
fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
// 请注意,这种方法存在许多缺点,
// 下个代码展示,会详细介绍如何提高这些命令的可移植性。
Command::new("gcc").args(&["src/hello.c", "-c", "-fPIC", "-o"])
.arg(&format!("{}/hello.o", out_dir))
.status().unwrap();
Command::new("ar").args(&["crus", "libhello.a", "hello.o"])
.current_dir(&Path::new(&out_dir))
.status().unwrap();
println!("cargo:rustc-link-search=native={}", out_dir);
println!("cargo:rustc-link-lib=static=hello");
}
此构建脚本首先将 C 文件编译为对象文件(通过调用gcc),然后将这个对象文件转换为静态库(通过调用ar),最后一步是反馈给 Cargo ,以表示我们的输出在out_dir和通过-l static=hello标志,编译器应该将箱静态链接到libhello.a。
请注意,这种硬编码方法有许多缺点:
这个gcc命令本身不是跨平台可移植的。如,在 Windows 平台不太可能gcc,甚至不是所有 UNIX 平台都可能有gcc。 这个ar命令也处于类似的情况。
这些命令不考虑跨编译。如果我们为 Android 这样的平台进行跨编译,gcc就不太可能产生一个可执行的 ARM.
但不要害怕,这里build-dependencies就帮到你! Cargo 生态系统有许多包,为了使此类任务更加容易、可移植和标准化。构建脚本可以写成:
// build.rs
// 依赖于外部维护的`cc`包,管理
// 调用C编译器。
extern crate cc;
fn main() {
cc::Build::new()
.file("src/hello.c")
.compile("hello");
}
添加cc箱,这样将构建,依赖cc就好啦,将下面的添加到您的Cargo.toml:
[build-dependencies]
cc = "1.0"
这个cc箱抽象了 C 代码构建,主要用于脚本需求范围:
它调用适当的编译器(Windows 的 MSVC,gcc对 MinGW ,cc对 UNIX 平台等等).
通过向正在使用的编译器传递适当的标志,获取TARGET变量.
其他环境变量,如OPT_LEVEL,DEBUG等等,都是自动处理的.
stdout 输出和OUT_DIR位置也由cc库控制.
在这里,我们可以开始看到,将尽可能多的功能移植到公共构建依赖项,而不是在所有构建脚本之间复制来复制去,的一些主要好处!
回到案例研究,让我们快速浏览一下src目录中的内容:
// src/hello.c
#include
void hello() {
printf("Hello, World!\n");
}
// src/main.rs
// 注意缺少`#[link]`属性。 我们选择,将责任委派给
// 构建脚本的链接,而不是硬编码
// 它在源文件中.
extern { fn hello(); }
fn main() {
unsafe { hello(); }
}
然后,就好啦! 这就完成了使用构建脚本,从 Cargo 包构建一些 C 代码的示例。这也说明了为什么在许多情况下使,用构建依赖项非常重要,甚至更加简洁!
我们还看到了构建脚本使用箱,纯粹作为用于构建过程的依赖项,而不是在运行时,用作箱本身的依赖项的简要示例。
Case study: Linking to system libraries
案例学习: 链接到系统库
这里的最后一个案例研究,将研究 Cargo 库如何链接到系统库,以及构建脚本如何支持这个用例。
通常,Rust 箱希望链接到系统上经常提供的本地库,以绑定其功能,或者只是将其用作实现细节的一部分。想以不管平台的方式执行这个操作,而这却是一个相当微妙的问题,再次说明下,构建脚本的目的是尽可能多地分配这些(微妙)内容,以便让消费者尽可能容易地使用它.
作为一个例子,让我们来看一个Cargo 本身的依赖,libgit2。这个 C 库其实有许多约束条件:
它可选为依赖 Unix 上的 OpenSSL ,来实现 https 传输.
它可选为依赖所有平台上的 libssh2 ,来实现 ssh 传输.
默认情况下,它通常不安装在所有系统上.
它可以从源代码使用cmake构建.
为了可视化这里发生的事情,让我们看一下,链接本机 C 库的相关 Cargo 包的清单。
[package]
name = "libgit2-sys"
version = "0.1.0"
authors = ["..."]
links = "git2"
build = "build.rs"
[dependencies]
libssh2-sys = { git = "https://github.com/alexcrichton/ssh2-rs" }
[target.'cfg(unix)'.dependencies]
openssl-sys = { git = "https://github.com/alexcrichton/openssl-sys" }
# ...
正如上面的清单所显示的,我们指定了一个build脚本,但值得注意的是,该示例具有links项,说明该箱(libgit2-sys)链接到了这个本地库git2。
在这里,我们还看到,我们选择让 Rust 箱有一个无条件的,通过libssh2-sys箱依赖libssh2(ssh2-rs),以及(有条件的)特定于平 unix 台的openssl-sys依赖(其他平台现在被漠视)。这似乎有点违反在 Cargo 清单 的 C 依赖 的明确性,但这实际上是这'地方'中使用 Cargo 的一种约定.
*-sys Packages
*-sys 包们
为了减轻对系统库的链接,crates.io 有一个包命名和功能的惯例。比如包名foo-sys,它应该提供两个主要功能:
库箱应链接到本地库libfoo。 在源代码最后构建之前,这将经常探测当前的系统的libfoo。
库箱应提供在libfoo的声明函数,但是不绑定或高级抽象。
一套*-sys包,提供了一组用于连接到本地库的公共依赖项。通过这种'本机库相关'的包约定,可以获得许多好处:
foo-sys的公共依赖,会减轻上面所说的,关于一个包的links的每个值规则。
一个公共依赖关系,更能发现libfoo本身的集中逻辑(或者从源代码构建它).
这些依赖关系很容易被重写.
Building libgit2
构建 libgit2 吧
现在我们已经整理了 libgit2 的依赖,我们需要实际编写下构建脚本。我们这里不讨论特定的代码片段,而只研究libgit2-sys构建脚本的高层细节。这并不是建议所有包都遵循这个策略,而仅概述一个特定的策略。
构建脚本应该做的第一步是查询 libgit2 是否已经安装在主机系统上。要做到这一点,我们将利用现有的工具pkg-config(当它可用时)。我们也会使用build-dependencies部分重构成pkg-config相关的所有代码(或者有人已经这样做了!)。
如果pkg-config找不到 libgit2,或者如果pkg-config只是没有安装,下一步就要从捆绑源代码构建 libgit2 (捆绑源码作为libgit2-sys本身的一部分)。然而,在这样做时有一些细微差别,我们需要加以考虑:
libgit2 的构建系统,cmake需要能够找到 libgit2 可选依赖 libssh2 。而我们确信我们已经构建了它(因它是一个 Cargo 依赖项),我们只需要传递这个信息。为此,我们利用元数据格式,在构建脚本之间传递信息。在这个例子中,打印出的 libssh2 包信息是cargo:root=...,它来告诉我们 libssh2 安装在哪里,然后我们可以通过CMAKE_PREFIX_PATH环境变量让 cmkae 知道。
我们需要处理下,编译 C 代码时的一些CFLAGS值(也要告诉cmake关于这个信息)。我们想传递的一些标志是 64 位的-m64,32 位的-m32,或-fPIC也适用于 64 位。
最后,我们调用cmake将所有输出放入环境变量OUT_DIR目录,然后打印必要的元数据,以指导 rustc 如何链接到 libgit2。
这个构建脚本的大部分功能,很容易就重构为常见的依赖项,因此我们的构建脚本不像这个描述那样长烦! 实际上,通过构建依赖项,构建脚本应该非常简单。
cargo 配置国内镜像源 - 知乎
cargo 配置国内镜像源 - 知乎切换模式写文章登录/注册cargo 配置国内镜像源葫芦娃救爷爷1、进入 $HOME/.cargo 文件夹中。我的目录是 C:\Users\admin\.cargo2、删除一个名为 .package-cache 的文件3、创建一个名为 config 的文件,注意不要后缀4、编辑 config 文件,将下面内容添加进去后,保存退出即可[source.crates-io]
replace-with = 'sjtu' # 指定使用下面哪个源,修改为source.后面的内容即可
# 中国科学技术大学
[source.ustc]
registry = "https://mirrors.ustc.edu.cn/crates.io-index"
# 上海交通大学
[source.sjtu]
registry = "https://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index/"
# 清华大学
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
# rustcc社区
[source.rustcc]
registry = "https://code.aliyun.com/rustcc/crates.io-index.git"5、进入项目目录,运行命令 cargo build 就可以了发布于 2023-12-15 09:52・IP 属地四川Cargo(Rust)赞同添加评论分享喜欢收藏申请
创建一个新的包 - Cargo 手册 中文版
创建一个新的包 - Cargo 手册 中文版
简介1. 入门1.1. 安装1.2. 初次使用 Cargo2. Cargo 指南2.1. 为什么存在 Cargo2.2. 创建一个新的包2.3. 处理现有包2.4. 依赖2.5. 项目布局2.6. Cargo.toml 和 Cargo.lock2.7. 测试2.8. 持续集成2.9. Cargo Home2.10. 构建缓存3. Cargo 参考3.1. 指定依赖3.1.1. Overriding Dependencies3.2. 清单格式3.2.1. Cargo Targets3.3. Workspaces3.4. Features3.4.1. Features Examples3.5. Profiles3.6. 设置3.7. 环境变量3.8. 构建脚本3.8.1. Build Script Examples3.9. 将 crate 发布到 Crates.io3.10. 包 ID 规格3.11. 更换源3.12. 外部工具3.13. Registries3.14. Dependency Resolution3.15. SemVer Compatibility3.16. 不稳定功能4. Cargo Commands4.1. General Commands4.1.1. cargo4.1.2. cargo help4.1.3. cargo version4.2. Build Commands4.2.1. cargo bench4.2.2. cargo build4.2.3. cargo check4.2.4. cargo clean4.2.5. cargo doc4.2.6. cargo fetch4.2.7. cargo fix4.2.8. cargo run4.2.9. cargo rustc4.2.10. cargo rustdoc4.2.11. cargo test4.3. Manifest Commands4.3.1. cargo generate-lockfile4.3.2. cargo locate-project4.3.3. cargo metadata4.3.4. cargo pkgid4.3.5. cargo tree4.3.6. cargo update4.3.7. cargo vendor4.3.8. cargo verify-project4.4. Package Commands4.4.1. cargo init4.4.2. cargo install4.4.3. cargo new4.4.4. cargo search4.4.5. cargo uninstall4.5. Publishing Commands4.5.1. cargo login4.5.2. cargo owner4.5.3. cargo package4.5.4. cargo publish4.5.5. cargo yank5. 参见问题6. 附录:术语白7. Appendix: Git Authentication
Light (default)
Rust
Coal
Navy
Ayu
Cargo 手册 中文版
创建一个新项目
要使用 Cargo 启动新项目,请使用cargo new:
$ cargo new hello_world --bin
我们传递--bin,是因为我们正在制作一个二进制程序(默认): 如果我们正在创建一个库(lib),我们就会把传递--lib。默认情况下,这个目录会初始化为一个新的git存储库,如果您不希望它这样做,请传递--vcs none。
让我们来看看 Cargo 为我们带来了什么:
$ cd hello_world
$ tree .
.
├── Cargo.toml
└── src
└── main.rs
1 directory, 2 files
这就是我们开始所需要的一切首。先让我们看看Cargo.toml:
[package]
name = "hello_world"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
[dependencies]
这被称为一个manifest元清单,它包含了 Cargo 编译项目所需的所有元数据.
那src/main.rs有啥:
fn main() {
println!("Hello, world!");
}
Cargo 为我们创造了一个"hello_world".我们来编译它:
$ cargo build
Compiling hello_world v0.1.0 (file:///path/to/project/hello_world)
然后运行它:
$ ./target/debug/hello_world
Hello, world!
我们也可以直接使用cargo run,它会自行编译,然后运行它, 一步到位:
$ cargo run
Fresh hello_world v0.1.0 (file:///path/to/project/hello_world)
Running `target/hello_world`
Hello, world!
您会注意到已创建了几个新文件和目录:
$ tree .
.
|-- Cargo.lock
|-- Cargo.toml
|-- src
| `-- main.rs
`-- target
`-- debug
|-- build
|-- deps
| |-- hello_world-6ad0b2df81336e7f
| |-- hello_world-6ad0b2df81336e7f.d
| `-- hello_world-6ad0b2df81336e7f.dSYM
| `-- Contents
| |-- Info.plist
| `-- Resources
| `-- DWARF
| `-- hello_world-6ad0b2df81336e7f
|-- examples
|-- hello_world
|-- hello_world.d
|-- hello_world.dSYM -> deps/hello_world-6ad0b2df81336e7f.dSYM
|-- incremental
| // ...
`-- native
15 directories, 19 files
这个Cargo.lock文件啊,是包含我们的依赖项的有关信息(即便还没有依赖),其内容看起来可不是很有趣啊。再有就是target目录包含所有构建产品(二进制文件..),并且,可以看出,Cargo 默认生成调试(debug)版本。您可以使用cargo build --release,这会在开启优化的情况下,编译文件:
$ cargo build --release
Compiling hello_world v0.1.0 (file:///path/to/project/hello_world)
cargo build --release将结果二进制文件放入target/release,而不再是target/debug目录.
调试模式的编译是开发的默认设置 - 编译时间较短,因为编译器不进行优化,但代码运行速度较慢。发布(release)模式编译需要更长时间,但代码运行速度更快。
CARGO中文(简体)翻译:剑桥词典
CARGO中文(简体)翻译:剑桥词典
词典
翻译
语法
同义词词典
+Plus
剑桥词典+Plus
Shop
剑桥词典+Plus
我的主页
+Plus 帮助
退出
剑桥词典+Plus
我的主页
+Plus 帮助
退出
登录
/
注册
中文 (简体)
查找
查找
英语-中文(简体)
cargo 在英语-中文(简体)词典中的翻译
cargonoun [ C or U ] uk
Your browser doesn't support HTML5 audio
/ˈkɑː.ɡəʊ/ us
Your browser doesn't support HTML5 audio
/ˈkɑːr.ɡoʊ/ plural cargoes or cargos
Add to word list
Add to word list
C2 the goods carried by a ship, aircraft, or other large vehicle
(轮船、飞机等大型交通工具装载的)货物
a cargo ship/plane
货船/货运飞机
The ship was carrying a cargo of wood.
这艘船正在运送一批木材。
同义词
consignment
load
payload
shipment比较
freight noun
更多范例减少例句cargo vesselsSmoke detectors are now installed in cargo holds.More than half the ship's 130,000-ton cargo of crude oil has already been spilledThis is where the planes would land and unload their cargo.Nuclear materials are currently carried on both cargo and passenger flights.
(cargo在剑桥英语-中文(简体)词典的翻译 © Cambridge University Press)
cargo的例句
cargo
It has become increasingly appreciated that, in addition to their cargo of ice and dust, comets also contain a large amount of organic material.
来自 Cambridge English Corpus
The cargo hold capacity and speed of the ship are decided by the requirement to transpor t a given yearly cargo quantity.
来自 Cambridge English Corpus
Another, albeit less obvious, indication for such an overlap may be adduced from the fairly low frequency of trips in the cargo-carrying sector.
来自 Cambridge English Corpus
However, it will be restricted by cargo terminal capacity and classification's rule because excessive-sized depth produces poor stability per formance in calm water and waves.
来自 Cambridge English Corpus
The agents therefore represent geographical regions, while cargo load is modelled as information (objects) flowing between agents.
来自 Cambridge English Corpus
A piece of cargo can be loaded into the rocket if the rocket and the cargo are in the same location.
来自 Cambridge English Corpus
But the generalisation leaves out cargo (43%) on the one hand and flamingo (27%) on the other.
来自 Cambridge English Corpus
When dealing with cargo, the standardization of the units of measurement was much more complicated.
来自 Cambridge English Corpus
示例中的观点不代表剑桥词典编辑、剑桥大学出版社和其许可证颁发者的观点。
C2
cargo的翻译
中文(繁体)
(輪船、飛機等大型交通工具裝載的)貨物…
查看更多内容
西班牙语
cargamento, carga, carga [feminine]…
查看更多内容
葡萄牙语
carregamento, carga [feminine]…
查看更多内容
更多语言
in Marathi
日语
土耳其语
法语
加泰罗尼亚语
in Dutch
in Tamil
in Hindi
in Gujarati
丹麦语
in Swedish
马来语
德语
挪威语
in Urdu
in Ukrainian
俄语
in Telugu
阿拉伯语
in Bengali
捷克语
印尼语
泰语
越南语
波兰语
韩语
意大利语
माल, जहाज, विमान किंवा इतर मोठ्या वाहनातून ज्या वस्तूंची ने आण होते त्या वस्तू…
查看更多内容
貨物, 積荷(つみに)…
查看更多内容
yük, yük eşyası, kargo…
查看更多内容
cargaison [feminine], chargement [masculine], cargaison…
查看更多内容
càrrega…
查看更多内容
lading…
查看更多内容
ஒரு கப்பல், விமானம் அல்லது பிற பெரிய வாகனம் மூலம் கொண்டு செல்லப்படும் பொருட்கள்…
查看更多内容
(जहाज़, विमान या नया बड़े वाहन का) कार्गो, जहाज़ी माल…
查看更多内容
માલ, સામાન…
查看更多内容
last, ladning…
查看更多内容
[skepps]last…
查看更多内容
kargo…
查看更多内容
die Fracht…
查看更多内容
last [masculine], last…
查看更多内容
بحری یا ہوائی جہاز یا کسی اور گاڑی پر لادا جانے والا سامان…
查看更多内容
вантаж корабля…
查看更多内容
груз…
查看更多内容
సరుకు…
查看更多内容
بَضائع…
查看更多内容
মালবাহী…
查看更多内容
náklad (lodní)…
查看更多内容
muatan…
查看更多内容
สินค้าที่บรรทุก…
查看更多内容
hàng hóa chở trên tàu thủy hoặc máy bay…
查看更多内容
towar, ładunek…
查看更多内容
화물…
查看更多内容
carico…
查看更多内容
需要一个翻译器吗?
获得快速、免费的翻译!
翻译器工具
cargo的发音是什么?
在英语词典中查看 cargo 的释义
浏览
caretaker government
caretaking
careworn
carfare
cargo
cargo bike
cargo pants
carhop
Caribbean
cargo更多的中文(简体)翻译
全部
cargo bike
cargo pants
cargo pants, at combat trousers
cargo bike, at freight bike
查看全部意思»
“每日一词”
veggie burger
UK
Your browser doesn't support HTML5 audio
/ˈvedʒ.i ˌbɜː.ɡər/
US
Your browser doesn't support HTML5 audio
/ˈvedʒ.i ˌbɝː.ɡɚ/
a type of food similar to a hamburger but made without meat, by pressing together small pieces of vegetables, seeds, etc. into a flat, round shape
关于这个
博客
Forget doing it or forget to do it? Avoiding common mistakes with verb patterns (2)
March 06, 2024
查看更多
新词
stochastic parrot
March 04, 2024
查看更多
已添加至 list
回到页面顶端
内容
英语-中文(简体)例句翻译
©剑桥大学出版社与评估2024
学习
学习
学习
新词
帮助
纸质书出版
Word of the Year 2021
Word of the Year 2022
Word of the Year 2023
开发
开发
开发
词典API
双击查看
搜索Widgets
执照数据
关于
关于
关于
无障碍阅读
剑桥英语教学
剑桥大学出版社与评估
授权管理
Cookies与隐私保护
语料库
使用条款
京ICP备14002226号-2
©剑桥大学出版社与评估2024
剑桥词典+Plus
我的主页
+Plus 帮助
退出
词典
定义
清晰解释自然的书面和口头英语
英语
学习词典
基础英式英语
基础美式英语
翻译
点击箭头改变翻译方向。
双语词典
英语-中文(简体)
Chinese (Simplified)–English
英语-中文(繁体)
Chinese (Traditional)–English
英语-荷兰语
荷兰语-英语
英语-法语
法语-英语
英语-德语
德语-英语
英语-印尼语
印尼语-英语
英语-意大利语
意大利语-英语
英语-日语
日语-英语
英语-挪威语
挪威语-英语
英语-波兰语
波兰语-英语
英语-葡萄牙语
葡萄牙语-英语
英语-西班牙语
西班牙语-英语
English–Swedish
Swedish–English
半双语词典
英语-阿拉伯语
英语-孟加拉语
英语-加泰罗尼亚语
英语-捷克语
英语-丹麦语
English–Gujarati
英语-印地语
英语-韩语
英语-马来语
英语-马拉地语
英语-俄语
English–Tamil
English–Telugu
英语-泰语
英语-土耳其语
英语-乌克兰语
English–Urdu
英语-越南语
翻译
语法
同义词词典
Pronunciation
剑桥词典+Plus
Shop
剑桥词典+Plus
我的主页
+Plus 帮助
退出
登录 /
注册
中文 (简体)
Change
English (UK)
English (US)
Español
Русский
Português
Deutsch
Français
Italiano
中文 (简体)
正體中文 (繁體)
Polski
한국어
Türkçe
日本語
Tiếng Việt
हिंदी
தமிழ்
తెలుగు
关注我们
选择一本词典
最近的词和建议
定义
清晰解释自然的书面和口头英语
英语
学习词典
基础英式英语
基础美式英语
语法与同义词词典
对自然书面和口头英语用法的解释
英语语法
同义词词典
Pronunciation
British and American pronunciations with audio
English Pronunciation
翻译
点击箭头改变翻译方向。
双语词典
英语-中文(简体)
Chinese (Simplified)–English
英语-中文(繁体)
Chinese (Traditional)–English
英语-荷兰语
荷兰语-英语
英语-法语
法语-英语
英语-德语
德语-英语
英语-印尼语
印尼语-英语
英语-意大利语
意大利语-英语
英语-日语
日语-英语
英语-挪威语
挪威语-英语
英语-波兰语
波兰语-英语
英语-葡萄牙语
葡萄牙语-英语
英语-西班牙语
西班牙语-英语
English–Swedish
Swedish–English
半双语词典
英语-阿拉伯语
英语-孟加拉语
英语-加泰罗尼亚语
英语-捷克语
英语-丹麦语
English–Gujarati
英语-印地语
英语-韩语
英语-马来语
英语-马拉地语
英语-俄语
English–Tamil
English–Telugu
英语-泰语
英语-土耳其语
英语-乌克兰语
English–Urdu
英语-越南语
词典+Plus
词汇表
选择语言
中文 (简体)
English (UK)
English (US)
Español
Русский
Português
Deutsch
Français
Italiano
正體中文 (繁體)
Polski
한국어
Türkçe
日本語
Tiếng Việt
हिंदी
தமிழ்
తెలుగు
内容
英语-中文(简体)
Noun
例句
Translations
语法
所有翻译
我的词汇表
把cargo添加到下面的一个词汇表中,或者创建一个新词汇表。
更多词汇表
前往词汇表
对该例句有想法吗?
例句中的单词与输入词条不匹配。
该例句含有令人反感的内容。
取消
提交
例句中的单词与输入词条不匹配。
该例句含有令人反感的内容。
取消
提交
cargo是什么意思_cargo的翻译_音标_读音_用法_例句_爱词霸在线词典
o是什么意思_cargo的翻译_音标_读音_用法_例句_爱词霸在线词典首页翻译背单词写作校对词霸下载用户反馈专栏平台登录cargo是什么意思_cargo用英语怎么说_cargo的翻译_cargo翻译成_cargo的中文意思_cargo怎么读,cargo的读音,cargo的用法,cargo的例句翻译人工翻译试试人工翻译翻译全文简明柯林斯牛津cargo高中/CET4/CET6/考研/GRE/TOEFL英 [ˈkɑːɡəʊ]美 [ˈkɑːrɡoʊ]释义n.(船或飞机装载的)货物大小写变形:Cargo点击 人工翻译,了解更多 人工释义词态变化复数: cargoes;实用场景例句全部货物The tanker began to spill its cargo of oil.油轮已开始漏油。牛津词典a cargo ship货船牛津词典The boat calls at the main port to load its regular cargo of bananas.船停靠这个主要港口以装载其常规货物——香蕉。柯林斯高阶英语词典...cargo planes.货运飞机柯林斯高阶英语词典How many boys and girls are there in your class?你班有多少男生和女生?期刊摘选We had to lash the cargo to the ship's deck during the storm.在暴风雨中,我们不得不把货物紧系在船的甲板上.《简明英汉词典》The revival of the railway service will be immensely beneficial for the speedy movement of passengers and cargo.铁路运输的复兴对加快客货运送大有助益.《简明英汉词典》The cargo has been transshipped into a river steamer.货物已载入一艘内河轮船上.《现代英汉综合大词典》The ship's cargo is stowed in the hold.船的货物都装好放在舱里.《简明英汉词典》The sailors hoisted the cargo onto the deck.水手们把货物吊到甲板上.《简明英汉词典》We have to make three loads of the cargo.我们得把货物分成3批装运.《现代汉英综合大词典》The vessel is fully loaded with cargo for Shanghai.这艘船满载货物驶往上海.《现代汉英综合大词典》The cargo was soon hoisted into the ship.货物很快就被吊进船舱.《用法词典》This port can handle up to 50000000 tons of cargo every year.这个港口每年可吞吐50000000吨货物.《现代汉英综合大词典》The cargo on board have been damaged by sea water.船上的货物因水湿而残损.《现代汉英综合大词典》Passengers with cargo must embark first.带货物的乘客先上船.《简明英汉词典》Then the sound of rumbling thunder reached the watchers on the shore, as cargo, ballast, ammunition and 400 people went sliding and crashing down to the port side of the steeply listing ship.后来当船上的货物 、 压舱物 、 弹药以及400个人轰的一声滑到急剧倾斜的船体左舷一侧的时候, 岸上观看的人们听到了雷鸣般的轰隆声.《用法词典》The cargo has shifted.船货移动了位置.《现代汉英综合大词典》The cargo vessel will arrive at the port within the next few days.那艘货船不日即可抵港.《现代汉英综合大词典》The cargo is being discharged into lighters.正在往驳船里卸货.《现代汉英综合大词典》The ship has a cargo of about 200 ton.这条船大约有200吨的货物.《简明英汉词典》The cargo hasn't all been unloaded yet.船上的货还没下完.《现代汉英综合大词典》He was instructed to inspect the cargo on the ship and detain such cargo if necessary.他奉命去检查船上所载货物并于必要时扣留该项货物.《现代英汉综合大词典》This port can handle up to 40,000,000 tons of cargo a year.这个港口一年可吞吐4,000万吨货物.《现代汉英综合大词典》Did you reckon in the time needed for unloading the cargo?你把卸货所需的时间估计进去了 吗 ?《现代汉英综合大词典》The freighter carries a few passengers in addition to its cargo.这艘货船除了货物之外还载有一些乘客.《简明英汉词典》Once the cargo has been shipped, insurance can be covered instantaneously.货物一旦发出, 我会立即投保.《简明英汉词典》The cargo includes three thousand pieces of porcelain.这批货物中包括三千件瓷器.《简明英汉词典》Fire may have breached the cargo tanks and set the oil ablaze.大火当时有可能把货船上的油罐烧漏了,从而引燃原油。柯林斯例句收起实用场景例句真题例句全部考研Cargo aircraft, in contrast,might be easier to reschedule,as might routine military flight.2010年考研真题(英语二)阅读理解 Section Ⅱ收起真题例句英英释义Noun1. goods carried by a large vehicle收起英英释义词根词缀词根: car=load,表示"装载,负担"n.car 汽车, 小汽车, 车辆, 客车, [铁]车厢car装载,负担→n.汽车, 小汽车, 车辆, 客车, [铁]车厢career 事业, 生涯car车+eer人→愿意为车辆经过的道路→人所经历的路程→事业, 生涯cargo 船货, [车、船、飞机等运输的]货物car装载,负担+go→n.船货, [车、船、飞机等运输的]货物carpenter 木匠car装载,负担+pent+er人→n.木匠carriage 马车, 客车, 运费carry携带, 运送+age物品→马车, 客车carrier 运送者, 邮递员, 带菌者carry携带, 运送+er人→carrier运送者, 带菌者cart 大车, 手推车car装载,负担+t→n.大车, 手推车v.carry 携带, 运送, 支持, 支撑, 传送, 意味car装载,负担+ry→携带, 运送同义词辨析burden, cargo, load这些名词均有"负担,负荷"之意。burden: 指沉重、令人不快的负担;指精神负担时常有不堪忍受的意味。cargo: 指用轮船、车辆或飞机长距离运载的货物。load: 普通用词,含义广泛,指人、畜、车、船等负载的东西,指精神负担时,可与burden换用,但无感情色彩。同义词n.货物shipmentloadfreight其他释义freightloadshipment行业词典法律货物 释义词态变化实用场景例句真题例句英英释义词根词缀同义词辨析同义词行国泰货运 - 主页 | 国泰
国泰货运 - 主页 | 国泰
You need to enable JavaScript to run this app.
Rust 包管理器 Cargo 入门 - 知乎
Rust 包管理器 Cargo 入门 - 知乎首发于开源开发切换模式写文章登录/注册Rust 包管理器 Cargo 入门Linux中国已认证账号了解 Rust 的软件包管理器和构建工具。-- Gaurav Kamathe(作者)Rust 是一种现代编程语言,可提供高性能、可靠性和生产力。几年来,它一直被 StackOverflow 调查评为最受欢迎的语言。除了是一种出色的编程语言之外,Rust 还具有一个称为 Cargo 的构建系统和软件包管理器。Cargo 处理许多任务,例如构建代码、下载库或依赖项等等。这两者捆绑在一起,因此在安装 Rust 时会得到 Cargo。安装 Rust 和 Cargo在开始之前,你需要安装 Rust 和 Cargo。Rust 项目提供了一个可下载的脚本来处理安装。要获取该脚本,请打开浏览器以访问 https://sh.rustup.rs 并保存该文件。阅读该脚本以确保你对它的具体行为有所了解,然后再运行它:$ sh ./rustup.rs你也可以参考这个安装 Rust 的网页以获取更多信息。安装 Rust 和 Cargo 之后,你必须 获取(source) env 文件中的配置:$ source $HOME/.cargo/env更好的办法是,将所需目录添加到 PATH 环境变量中:export PATH=$PATH:~/.cargo/bin如果你更喜欢使用软件包管理器(例如 Linux 上的 DNF 或 Apt),请在发行版本的存储库中查找 Rust 和 Cargo 软件包,并进行相应的安装。 例如:$ dnf install rust cargo安装并设置它们后,请验证你拥有的 Rust 和 Cargo 版本:$ rustc --version
rustc 1.41.0 (5e1a79984 2020-01-27)
$ cargo --version
cargo 1.41.0 (626f0f40e 2019-12-03)手动构建和运行 Rust从在屏幕上打印“Hello, world!”的简单程序开始。打开你喜欢的文本编辑器,然后键入以下程序:$ cat hello.rs
fn main() {
println!("Hello, world!");
}以扩展名 .rs 保存文件,以将其标识为 Rust 源代码文件。使用 Rust 编译器 rustc 编译程序:$ rustc hello.rs编译后,你将拥有一个与源程序同名的二进制文件:$ ls -l
total 2592
-rwxr-xr-x. 1 user group 2647944 Feb 13 14:14 hello
-rw-r--r--. 1 user group 45 Feb 13 14:14 hello.rs
$执行程序以验证其是否按预期运行:$ ./hello
Hello, world!这些步骤对于较小的程序或任何你想快速测试的东西就足够了。但是,在进行涉及到多人的大型程序时,Cargo 是前进的最佳之路。使用 Cargo 创建新包Cargo 是 Rust 的构建系统和包管理器。它可以帮助开发人员下载和管理依赖项,并帮助创建 Rust 包。在 Rust 社区中,Rust 中的“包”通常被称为“crate”(板条箱),但是在本文中,这两个词是可以互换的。请参阅 Rust 社区提供的 Cargo FAQ 来区分。如果你需要有关 Cargo 命令行实用程序的任何帮助,请使用 --help 或 -h 命令行参数:$ cargo –help要创建一个新的包,请使用关键字 new,跟上包名称。在这个例子中,使用 hello_opensource 作为新的包名称。运行该命令后,你将看到一条消息,确认 Cargo 已创建具有给定名称的二进制包:$ cargo new hello_opensource
Created binary (application) `hello_opensource` package运行 tree 命令以查看目录结构,它会报告已创建了一些文件和目录。首先,它创建一个带有包名称的目录,并且在该目录内有一个存放你的源代码文件的 src 目录:$ tree .
.
└── hello_opensource
├── Cargo.toml
└── src
└── main.rs
2 directories, 2 filesCargo 不仅可以创建包,它也创建了一个简单的 “Hello, world” 程序。打开 main.rs 文件看看:$ cat hello_opensource/src/main.rs
fn main() {
println!("Hello, world!");
}下一个要处理的文件是 Cargo.toml,这是你的包的配置文件。它包含有关包的信息,例如其名称、版本、作者信息和 Rust 版本信息。程序通常依赖于外部库或依赖项来运行,这使你可以编写应用程序来执行不知道如何编码或不想花时间编码的任务。你所有的依赖项都将在此文件中列出。此时,你的新程序还没有任何依赖关系。打开 Cargo.toml 文件并查看其内容:$ cat hello_opensource/Cargo.toml
[package]
name = "hello_opensource"
version = "0.1.0"
authors = ["user
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]使用 Cargo 构建程序到目前为止,一切都很顺利。现在你已经有了一个包,可构建一个二进制文件(也称为可执行文件)。在此之前,进入包目录:$ cd hello_opensource/你可以使用 Cargo 的 build 命令来构建包。注意消息说它正在“编译”你的程序:$ cargo build
Compiling hello_opensource v0.1.0 (/opensource/hello_opensource)
Finished dev [unoptimized + debuginfo] target(s) in 0.38s运行 build 命令后,检查项目目录发生了什么:$ tree .
.
├── Cargo.lock
├── Cargo.toml
├── src
│ └── main.rs
└── target
└── debug
├── build
├── deps
│ ├── hello_opensource-147b8a0f466515dd
│ └── hello_opensource-147b8a0f466515dd.d
├── examples
├── hello_opensource
├── hello_opensource.d
└── incremental
└── hello_opensource-3pouh4i8ttpvz
├── s-fkmhjmt8tj-x962ep-1hivstog8wvf
│ ├── 1r37g6m45p8rx66m.o
│ ├── 2469ykny0eqo592v.o
│ ├── 2g5i2x8ie8zed30i.o
│ ├── 2yrvd7azhgjog6zy.o
│ ├── 3g9rrdr4hyk76jtd.o
│ ├── dep-graph.bin
│ ├── query-cache.bin
│ ├── work-products.bin
│ └── wqif2s56aj0qtct.o
└── s-fkmhjmt8tj-x962ep.lock
9 directories, 17 files哇!编译过程产生了许多中间文件。另外,你的二进制文件将以与软件包相同的名称保存在 ./target/debug 目录中。使用 Cargo 运行你的应用程序现在你的二进制文件已经构建好了,使用 Cargo 的 run 命令运行它。如预期的那样,它将在屏幕上打印 Hello, world!。$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/hello_opensource`
Hello, world!或者,你可以直接运行二进制文件,该文件位于:$ ls -l ./target/debug/hello_opensource
-rwxr-xr-x. 2 root root 2655552 Feb 13 14:19 ./target/debug/hello_opensource如预期的那样,它产生相同的结果:$ ./target/debug/hello_opensource
Hello, world!假设你需要重建包,并丢弃早期编译过程创建的所有二进制文件和中间文件。Cargo 提供了一个方便的clean 选项来删除所有中间文件,但源代码和其他必需文件除外:$ cargo clean
$ tree .
.
├── Cargo.lock
├── Cargo.toml
└── src
└── main.rs
1 directory, 3 files对程序进行一些更改,然后再次运行以查看其工作方式。例如,下面这个较小的更改将 Opensource 添加到 Hello, world! 字符串中:$ cat src/main.rs
fn main() {
println!("Hello, Opensource world!");
}现在,构建该程序并再次运行它。这次,你会在屏幕上看到 Hello, Opensource world!:$ cargo build
Compiling hello_opensource v0.1.0 (/opensource/hello_opensource)
Finished dev [unoptimized + debuginfo] target(s) in 0.39s
$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/hello_opensource`
Hello, Opensource world!使用 Cargo 添加依赖项Cargo 允许你添加程序需要运行的依赖项。使用 Cargo 添加依赖项非常容易。每个 Rust 包都包含一个 Cargo.toml 文件,其中包含一个依赖关系列表(默认为空)。用你喜欢的文本编辑器打开该文件,找到 [dependencies] 部分,然后添加要包含在包中的库。例如,将 rand 库添加为依赖项:$ cat Cargo.toml
[package]
name = "hello_opensource"
version = "0.1.0"
authors = ["test user
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.3.14"试试构建你的包,看看会发生什么。$ cargo build
Updating crates.io index
Compiling libc v0.2.66
Compiling rand v0.4.6
Compiling rand v0.3.23
Compiling hello_opensource v0.1.0 (/opensource/hello_opensource)
Finished dev [unoptimized + debuginfo] target(s) in 4.48s现在,Cargo 会联系 Crates.io(这是 Rust 用于存储 crate(或包)的中央仓库),并下载和编译 rand。但是,等等 —— libc 包是怎么回事?你没有要安装 libc 啊。是的,rand 包依赖于 libc 包;因此,Cargo 也会下载并编译 libc。库的新版本会不断涌现,而 Cargo 提供了一种使用 update 命令更新其所有依赖关系的简便方法:cargo update你还可以选择使用 -p 标志跟上包名称来更新特定的库:cargo update -p rand使用单个命令进行编译和运行到目前为止,每当对程序进行更改时,都先使用了 build 之后是 run。有一个更简单的方法:你可以直接使用 run 命令,该命令会在内部进行编译并运行该程序。要查看其工作原理,请首先清理你的软件包目录:$ cargo clean
$ tree .
.
├── Cargo.lock
├── Cargo.toml
└── src
└── main.rs
1 directory, 3 files现在执行 run。输出信息表明它已进行编译,然后运行了该程序,这意味着你不需要每次都显式地运行 build:$ cargo run
Compiling hello_opensource v0.1.0 (/opensource/hello_opensource)
Finished dev [unoptimized + debuginfo] target(s) in 0.41s
Running `target/debug/hello_opensource`
Hello, world!在开发过程中检查代码在开发程序时,你经常会经历多次迭代。你需要确保你的程序没有编码错误并且可以正常编译。你不需要负担在每次编译时生成二进制文件的开销。Cargo 为你提供了一个 check 选项,该选项可以编译代码,但跳过了生成可执行文件的最后一步。首先在包目录中运行 cargo clean:$ tree .
.
├── Cargo.lock
├── Cargo.toml
└── src
└── main.rs
1 directory, 3 files现在运行 check 命令,查看对目录进行了哪些更改:$ cargo check
Checking hello_opensource v0.1.0 (/opensource/hello_opensource)
Finished dev [unoptimized + debuginfo] target(s) in 0.18s该输出显示,即使在编译过程中创建了中间文件,但没有创建最终的二进制文件或可执行文件。这样可以节省一些时间,如果该包包含了数千行代码,这非常重要:$ tree .
.
├── Cargo.lock
├── Cargo.toml
├── src
│ └── main.rs
└── target
└── debug
├── build
├── deps
│ ├── hello_opensource-842d9a06b2b6a19b.d
│ └── libhello_opensource-842d9a06b2b6a19b.rmeta
├── examples
└── incremental
└── hello_opensource-1m3f8arxhgo1u
├── s-fkmhw18fjk-542o8d-18nukzzq7hpxe
│ ├── dep-graph.bin
│ ├── query-cache.bin
│ └── work-products.bin
└── s-fkmhw18fjk-542o8d.lock
9 directories, 9 files要查看你是否真的节省了时间,请对 build 和 check 命令进行计时并进行比较。首先,计时 build 命令:$ time cargo build
Compiling hello_opensource v0.1.0 (/opensource/hello_opensource)
Finished dev [unoptimized + debuginfo] target(s) in 0.40s
real 0m0.416s
user 0m0.251s
sys 0m0.199s在运行 check 命令之前清理目录:$ cargo clean计时 check 命令:$ time cargo check
Checking hello_opensource v0.1.0 (/opensource/hello_opensource)
Finished dev [unoptimized + debuginfo] target(s) in 0.15s
real 0m0.166s
user 0m0.086s
sys 0m0.081s显然,check 命令要快得多。建立外部 Rust 包到目前为止,你所做的这些都可以应用于你从互联网上获得的任何 Rust crate。你只需要下载或克隆存储库,移至包文件夹,然后运行 build 命令,就可以了:git clone
cd
cargo build使用 Cargo 构建优化的 Rust 程序到目前为止,你已经多次运行 build,但是你注意到它的输出了吗?不用担心,再次构建它并密切注意:$ cargo build
Compiling hello_opensource v0.1.0 (/opensource/hello_opensource)
Finished dev [unoptimized + debuginfo] target(s) in 0.36s看到了每次编译后的 [unoptimized + debuginfo] 文本了吗?这意味着 Cargo 生成的二进制文件包含大量调试信息,并且未针对执行进行优化。开发人员经常经历开发的多次迭代,并且需要此调试信息进行分析。同样,性能并不是开发软件时的近期目标。因此,对于现在而言是没问题的。但是,一旦准备好发布软件,就不再需要这些调试信息。而是需要对其进行优化以获得最佳性能。在开发的最后阶段,可以将 --release 标志与 build 一起使用。仔细看,编译后,你应该会看到 [optimized] 文本:$ cargo build --release
Compiling hello_opensource v0.1.0 (/opensource/hello_opensource)
Finished release [optimized] target(s) in 0.29s如果愿意,你可以通过这种练习来了解运行优化软件与未优化软件时节省的时间。使用 Cargo 创建库还是二进制文件任何软件程序都可以粗略地分类为独立二进制文件或库。一个独立二进制文件也许即使是当做外部库使用的,自身也是可以运行的。但是,作为一个库,是可以被另一个独立二进制文件所利用的。到目前为止,你在本教程中构建的所有程序都是独立二进制文件,因为这是 Cargo 的默认设置。 要创建一个库,请添加 --lib 选项:$ cargo new --lib libhello
Created library `libhello` package这次,Cargo 不会创建 main.rs 文件,而是创建一个 lib.rs 文件。 你的库的代码应该是这样的:$ tree .
.
└── libhello
├── Cargo.toml
└── src
└── lib.rs
2 directories, 2 filesCargo 就是这样的,不要奇怪,它在你的新库文件中添加了一些代码。通过移至包目录并查看文件来查找添加的内容。默认情况下,Cargo 在库文件中放置一个测试函数。使用 Cargo 运行测试Rust 为单元测试和集成测试提供了一流的支持,而 Cargo 允许你执行以下任何测试:$ cd libhello/
$ cat src/lib.rs
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}Cargo 有一个方便的 test 命令,可以运行代码中存在的任何测试。尝试默认运行 Cargo 在库代码中放入的测试:$ cargo test
Compiling libhello v0.1.0 (/opensource/libhello)
Finished test [unoptimized + debuginfo] target(s) in 0.55s
Running target/debug/deps/libhello-d52e35bb47939653
running 1 test
test tests::it_works ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Doc-tests libhello
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out深入了解 Cargo 内部你可能有兴趣了解在运行一个 Cargo 命令时它底下发生了什么。毕竟,在许多方面,Cargo 只是个封装器。要了解它在做什么,你可以将 -v 选项与任何 Cargo 命令一起使用,以将详细信息输出到屏幕。这是使用 -v 选项运行 build 和 clean 的几个例子。在 build 命令中,你可以看到这些给定的命令行选项触发了底层的 rustc(Rust 编译器):$ cargo build -v
Compiling hello_opensource v0.1.0 (/opensource/hello_opensource)
Running `rustc --edition=2018 --crate-name hello_opensource src/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=147b8a0f466515dd -C extra-filename=-147b8a0f466515dd --out-dir /opensource/hello_opensource/target/debug/deps -C incremental=/opensource/hello_opensource/target/debug/incremental -L dependency=/opensource/hello_opensource/target/debug/deps`
Finished dev [unoptimized + debuginfo] target(s) in 0.36s而 clean 命令表明它只是删除了包含中间文件和二进制文件的目录:$ cargo clean -v
Removing /opensource/hello_opensource/target不要让你的技能生锈要扩展你的技能,请尝试使用 Rust 和 Cargo 编写并运行一个稍微复杂的程序。很简单就可以做到:例如,尝试列出当前目录中的所有文件(可以用 9 行代码完成),或者尝试自己回显输入。小型的实践应用程序可帮助你熟悉语法以及编写和测试代码的过程。本文为刚起步的 Rust 程序员提供了大量信息,以使他们可以开始入门 Cargo。但是,当你开始处理更大、更复杂的程序时,你需要对 Cargo 有更深入的了解。当你准备好迎接更多内容时,请下载并阅读 Rust 团队编写的开源的《Cargo 手册》,看看你可以创造什么!via: https://opensource.com/article/20/3/rust-cargo作者:Gaurav Kamathe 选题:lujun9972 译者:wxy 校对:wxy本文由 LCTT 原创编译,Linux中国 荣誉推出发布于 2020-03-09 10:54编程语言Rust(编程语言)赞同 301 条评论分享喜欢收藏申请转载文章被以下专栏收录开源开发计算机程序从打孔
cargo install - Cargo 手册 中文版
cargo install - Cargo 手册 中文版
简介1. 入门1.1. 安装1.2. 初次使用 Cargo2. Cargo 指南2.1. 为什么存在 Cargo2.2. 创建一个新的包2.3. 处理现有包2.4. 依赖2.5. 项目布局2.6. Cargo.toml 和 Cargo.lock2.7. 测试2.8. 持续集成2.9. Cargo Home2.10. 构建缓存3. Cargo 参考3.1. 指定依赖3.1.1. Overriding Dependencies3.2. 清单格式3.2.1. Cargo Targets3.3. Workspaces3.4. Features3.4.1. Features Examples3.5. Profiles3.6. 设置3.7. 环境变量3.8. 构建脚本3.8.1. Build Script Examples3.9. 将 crate 发布到 Crates.io3.10. 包 ID 规格3.11. 更换源3.12. 外部工具3.13. Registries3.14. Dependency Resolution3.15. SemVer Compatibility3.16. 不稳定功能4. Cargo Commands4.1. General Commands4.1.1. cargo4.1.2. cargo help4.1.3. cargo version4.2. Build Commands4.2.1. cargo bench4.2.2. cargo build4.2.3. cargo check4.2.4. cargo clean4.2.5. cargo doc4.2.6. cargo fetch4.2.7. cargo fix4.2.8. cargo run4.2.9. cargo rustc4.2.10. cargo rustdoc4.2.11. cargo test4.3. Manifest Commands4.3.1. cargo generate-lockfile4.3.2. cargo locate-project4.3.3. cargo metadata4.3.4. cargo pkgid4.3.5. cargo tree4.3.6. cargo update4.3.7. cargo vendor4.3.8. cargo verify-project4.4. Package Commands4.4.1. cargo init4.4.2. cargo install4.4.3. cargo new4.4.4. cargo search4.4.5. cargo uninstall4.5. Publishing Commands4.5.1. cargo login4.5.2. cargo owner4.5.3. cargo package4.5.4. cargo publish4.5.5. cargo yank5. 参见问题6. 附录:术语白7. Appendix: Git Authentication
Light (default)
Rust
Coal
Navy
Ayu
Cargo 手册 中文版
cargo-install(1)
NAME
cargo-install - Build and install a Rust binary
SYNOPSIS
cargo install [options] crate...
cargo install [options] --path path
cargo install [options] --git url [crate...]
cargo install [options] --list
DESCRIPTION
This command manages Cargo's local set of installed binary crates. Only
packages which have executable [[bin]] or [[example]] targets can be
installed, and all executables are installed into the installation root's
bin folder.
The installation root is determined, in order of precedence:
--root option
CARGO_INSTALL_ROOT environment variable
install.root Cargo config value
CARGO_HOME environment variable
$HOME/.cargo
There are multiple sources from which a crate can be installed. The default
location is crates.io but the --git, --path, and --registry flags can
change this source. If the source contains more than one package (such as
crates.io or a git repository with multiple crates) the crate argument is
required to indicate which crate should be installed.
Crates from crates.io can optionally specify the version they wish to install
via the --version flags, and similarly packages from git repositories can
optionally specify the branch, tag, or revision that should be installed. If a
crate has multiple binaries, the --bin argument can selectively install only
one of them, and if you'd rather install examples the --example argument can
be used as well.
If the package is already installed, Cargo will reinstall it if the installed
version does not appear to be up-to-date. If any of the following values
change, then Cargo will reinstall the package:
The package version and source.
The set of binary names installed.
The chosen features.
The profile (--profile).
The target (--target).
Installing with --path will always build and install, unless there are
conflicting binaries from another package. The --force flag may be used to
force Cargo to always reinstall the package.
If the source is crates.io or --git then by default the crate will be built
in a temporary target directory. To avoid this, the target directory can be
specified by setting the CARGO_TARGET_DIR environment variable to a relative
path. In particular, this can be useful for caching build artifacts on
continuous integration systems.
By default, the Cargo.lock file that is included with the package will be
ignored. This means that Cargo will recompute which versions of dependencies
to use, possibly using newer versions that have been released since the
package was published. The --locked flag can be used to force Cargo to use
the packaged Cargo.lock file if it is available. This may be useful for
ensuring reproducible builds, to use the exact same set of dependencies that
were available when the package was published. It may also be useful if a
newer version of a dependency is published that no longer builds on your
system, or has other problems. The downside to using --locked is that you
will not receive any fixes or updates to any dependency. Note that Cargo did
not start publishing Cargo.lock files until version 1.37, which means
packages published with prior versions will not have a Cargo.lock file
available.
OPTIONS
Install Options
--vers version
--version version
Specify a version to install. This may be a version
requirement, like ~1.2, to have Cargo
select the newest version from the given requirement. If the version does not
have a requirement operator (such as ^ or ~), then it must be in the form
MAJOR.MINOR.PATCH, and will install exactly that version; it is not
treated as a caret requirement like Cargo dependencies are.
--git url
Git URL to install the specified crate from.
--branch branch
Branch to use when installing from git.
--tag tag
Tag to use when installing from git.
--rev sha
Specific commit to use when installing from git.
--path path
Filesystem path to local crate to install.
--list
List all installed packages and their versions.
-f
--force
Force overwriting existing crates or binaries. This can be used if a package
has installed a binary with the same name as another package. This is also
useful if something has changed on the system that you want to rebuild with,
such as a newer version of rustc.
--no-track
By default, Cargo keeps track of the installed packages with a metadata file
stored in the installation root directory. This flag tells Cargo not to use or
create that file. With this flag, Cargo will refuse to overwrite any existing
files unless the --force flag is used. This also disables Cargo's ability to
protect against multiple concurrent invocations of Cargo installing at the
same time.
--bin name...
Install only the specified binary.
--bins
Install all binaries.
--example name...
Install only the specified example.
--examples
Install all examples.
--root dir
Directory to install packages into.
--registry registry
Name of the registry to use. Registry names are defined in Cargo config
files. If not specified, the default registry is used,
which is defined by the registry.default config key which defaults to
crates-io.
--index index
The URL of the registry index to use.
特性选择
可通过传递特性参数来控制启用哪些特性。如果没有给定要使用的特性,
则每个已选择的包都会自动使用default特性。
详见the features documentation。
--features features
传递以空格或者逗号分隔的列表,其中给出要启用的特性。工作区成员的特性可通过包名/特性名的语法启用。
此参数可多次给定,以分别启用给定的特性。
--all-features
为给定的包启用全部可用特性
--no-default-features
不启用给定包的default特性
Compilation Options
--target triple
为指定架构执行 Install 。默认情况下为本机的架构。三元组的格式为
可得到支持的构建目标列表。
也可通过build.target指定(config value)。
注意,指定该标志参数会使Cargo产生的构建工件放在与平常不同的目录下。
详情参见build cache
--target-dir directory
用于存放生成的工件以及中间文件的目录。也可通过环境变量CARGO_TARGET_DIR 或
build.target-dir config value指定。
默认情况下位于该平台临时目录下的一个新的临时文件夹。
当指定 --path标志参数时,若未指定--target-dir,则默认会使用工作区中的target目录。
--debug
Build with the dev profile instead the release profile.
See also the --profile option for choosing a specific profile by name.
--profile name
Install with the given profile.
See the the reference for more details on profiles.
Manifest Options
--frozen
--locked
这两个选项用于保证Cargo.lock文件是最新的。如果该锁文件不存在,或者不是最新的,Cargo
会报错退出。其中--frozen选项会阻止Cargo访问网络以检查锁文件是否是最新的。
这些选项,可用于保证Cargo.lock文件是最新的(比如持续集成的构建过程),
或用于避免联网。
--offline
禁止Cargo访问网络。如果不添加此选项,Cargo在需要访问网络但网络不可用的情况下,会报错
并停止工作。添加此选项后,Cargo会尽可能尝试不使用网络来工作。
注意,在此情况下可能会产生与联网状态下不同的依赖解析(Dependency Resolution)结果。
Cargo只会使用本地已下载的crate,即便本地的索引副本中表明可能有新版本crate。在离线前下载
所需依赖的方法,参见 cargo-fetch(1) 。
也可以通过 net.offline config value指定。
Miscellaneous Options
-j N
--jobs N
要并行运行的作业数量。也可通过build.jobs config value指定。
默认为CPU数量。
Display Options
-v
--verbose
启用更加详细的输出。可两次使用来显示"非常详细"的输出,其中包含了诸如 依赖警告 以及 构建脚本输出 等额外的输出内容。
也可通过term.verbose指定。
config value.
-q
--quiet
不输出Cargo的日志信息。也可通过term.quiet指定。
config value.
--color when
控制输出内容的颜色。有效取值如下:
auto (默认):自动检测终端是否支持带颜色的输出。
always:总显示带颜色的输出。
never:从不显示带颜色的输出。
也可通过term.color指定。
config value.
--message-format fmt
The output format for diagnostic messages. Can be specified multiple times
and consists of comma-separated values. Valid values:
human (default): Display in a human-readable text format. Conflicts with
short and json.
short: Emit shorter, human-readable text messages. Conflicts with human
and json.
json: Emit JSON messages to stdout. See
the reference
for more details. Conflicts with human and short.
json-diagnostic-short: Ensure the rendered field of JSON messages contains
the "short" rendering from rustc. Cannot be used with human or short.
json-diagnostic-rendered-ansi: Ensure the rendered field of JSON messages
contains embedded ANSI color codes for respecting rustc's default color
scheme. Cannot be used with human or short.
json-render-diagnostics: Instruct Cargo to not include rustc diagnostics in
in JSON messages printed, but instead Cargo itself should render the
JSON diagnostics coming from rustc. Cargo's own JSON diagnostics and others
coming from rustc are still emitted. Cannot be used with human or short.
Common Options
+toolchain
If Cargo has been installed with rustup, and the first argument to cargo
begins with +, it will be interpreted as a rustup toolchain name (such
as +stable or +nightly).
See the rustup documentation
for more information about how toolchain overrides work.
-h
--help
Prints help information.
-Z flag
Unstable (nightly-only) flags to Cargo. Run cargo -Z help for details.
环境
关于Cargo所读取的环境变量,可参见the reference
退出状态
0: Cargo命令执行成功
101: Cargo命令未能完成.
EXAMPLES
Install or upgrade a package from crates.io:
cargo install ripgrep
Install or reinstall the package in the current directory:
cargo install --path .
View the list of installed packages:
cargo install --list
SEE ALSO
cargo(1), cargo-uninstall(1), cargo-search(1), cargo-publish(1)