深入理解Rust中Cargo的使用
<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>概述</li><li>新建项目</li><li>添加依赖</li><li>添加测试</li><li>工作空间</li><li>文档生成</li><li>Cargo构建和编译项目Cargo build、Cargo run、</li><li>Cargo check仅编译项目</li><li>总结</li></ul></div><p class="maodian"></p><h2>概述</h2><p>在Rust生态系统中,Cargo扮演着至关重要的角色,它是官方的构建系统和包管理器。Cargo简化了项目的构建过程,提供了依赖项管理,以及一系列方便的工作流程工具,极大提升了开发效率和协作体验。</p>
<p class="maodian"></p><h2>新建项目</h2>
<p>新建Rust项目时,首先使用cargo new命令:cargo new my_project。这将在当前目录下生成一个名为my_project的目录,其中包括源码文件夹src、项目配置文件Cargo.toml等。Cargo.toml是Rust项目的核心配置文件,由TOML格式编写,用于描述项目的基本信息、依赖项以及其他项目特定的配置。</p>
<div class="jb51code"><pre class="brush:plain;">
name = "hello_rust"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html</pre></div>
<p class="maodian"></p><h2>添加依赖</h2>
<p>要在项目中添加依赖,只需在Cargo.toml中指定库名和版本即可。执行cargo build或cargo run命令后,Cargo会自动解析依赖关系,下载所需的依赖包,并编译项目的源代码。构建完成后,可以在target/debug或target/release目录下找到生成的可执行文件或库文件。</p>
<div class="jb51code"><pre class="brush:plain;">
rand = "0.8" # 添加随机数生成库rand,版本号为0.8
clap = "3.0"</pre></div>
<p class="maodian"></p><h2>添加测试</h2>
<p>Cargo内置了测试框架,我们可以编写测试代码来验证项目的正确性。在Rust项目中,测试文件通常放在tests目录下,并以_test.rs为后缀。比如:可以创建一个名为project_test.rs的测试文件,并编写以下测试代码。</p>
<div class="jb51code"><pre class="brush:plain;">fn add(a: i32, b: i32) -> i32 {
return a + b;
}
#
fn test_add() {
assert_eq!(add(2, 3), 5);
assert_eq!(add(-1, 1), 0);
}</pre></div>
<p>在Rust中,#是一个属性宏,用于标识一个函数为测试函数。当运行cargo test命令时,Rust会自动执行所有标记了#属性的函数,用于验证代码的正确性。</p>
<p class="maodian"></p><h2>工作空间</h2>
<p>在Rust中,工作空间是一种组织多个相关Cargo项目的结构。工作空间允许我们在一个顶级的Cargo.toml文件下管理多个库和可执行程序项目,便于共享依赖项、统一构建配置以及更好地组织项目结构。</p>
<p>要创建一个Rust工作空间,首先在根目录下创建一个顶级的Cargo.toml文件,并在其中加入配置节。</p>
<div class="jb51code"><pre class="brush:plain;"># Cargo.toml (顶级工作空间配置文件)
members = [
"crate1",
"crate2",
"bin1",
]</pre></div>
<p>在上述配置中:表明这是一个工作空间,members列表包含了工作空间中的成员项目,它们是相对于此顶级Cargo.toml文件的路径。每个成员项目(比如:crate1、crate2、bin1)都有自己的Cargo.toml文件,可以独立地定义自己的依赖项和构建目标。然而,共享的依赖项和配置将从顶级工作空间的Cargo.toml继承。</p>
<p>以crate1项目为例,其结构可能如下。</p>
<div class="jb51code"><pre class="brush:plain;">root_workspace/
├── Cargo.toml (顶级工作空间配置)
├── crate1/
│ ├── Cargo.toml (crate1的配置)
│ └── src/
│ └── lib.rs
├── crate2/
│ ├── Cargo.toml (crate2的配置)
│ └── src/
│ └── lib.rs
└── bin1/
├── Cargo.toml (bin1的配置,一个可执行项目)
└── src/
└── main.rs</pre></div>
<p>在这样的工作空间中,我们可以通过cargo test、cargo build或cargo run等命令在顶层目录执行操作。Cargo将会遍历所有成员项目,并执行相应的构建或测试任务。</p>
<p class="maodian"></p><h2>文档生成</h2>
<p>在Rust中,用于生成文档的标准工具是rustdoc,它是Rust编译器的一部分。rustdoc不仅可以提取代码中的注释来生成API文档,还支持编写Markdown和一些特殊的Rustdoc标记来丰富文档内容。</p>
<p>要为Rust项目生成文档,可按照以下步骤操作。</p>
<p>1、在Rust源代码中,使用///开始的三斜杠注释来编写函数、模块、结构体、枚举等的文档注释。</p>
<div class="jb51code"><pre class="brush:plain;">/// This is an example of a library function.
///
/// # Example
///
/// ```
/// use my_crate::print_text;
///
/// print_text("CSDN");
/// ```
pub fn print_text(text: &str) {
println!("{}", format!("{} CSDN", text));
}
fn main() {
print_text("Hello");
}</pre></div>
<p>2、打开终端,导航到项目的根目录(包含Cargo.toml的地方),然后运行:cargo doc。这个命令会编译项目并为所有公共接口生成文档,输出结果默认位于target/doc目录下。打开target/doc/<crate_name>/index.html文件,即可查看生成的文档。</p>
<p>3、如果要在本地启动一个Web服务器实时预览文档,可在生成文档后执行:cargo doc --open,这样rustdoc将自动在浏览器中打开文档索引页。</p>
<p class="maodian"></p><h2>Cargo构建和编译项目Cargo build、Cargo run、</h2>
<div class="jb51code"><pre class="brush:plain;">PS E:\100rust\hello_world> cargo build
Compiling hello_world v0.1.0 (E:\100rust\hello_world)
Finished dev target(s) in 1.48s
</pre></div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202404/2024040109291311.png" /></p>
<p>Cargo build会自动构建项目,自动创建一个target文件夹,文件夹包含编译后的文件:</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202404/2024040109291312.png" /></p>
<p>可以看到里面有个hello_world.exe文件,此时可以直接调用它,成功运行后输出helloworld。</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202404/2024040109291313.png" /></p>
<p>但是,我们可以使用Cargo run来一键编译且运行:</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202404/2024040109291314.png" /></p>
<p class="maodian"></p><h2>Cargo check仅编译项目</h2>
<p>有一个常会遇到的场景是,我在编程序时只想检查项目,但并不想生成可执行文件,我的目的是确保没有错误或者bug。因为,通常编译且生成可执行文件,需要的时间会大于仅编译检查的时间,所以使用Cargo check会更快,如果你使用Cargo run当然也是可行的,但我相信你不会这么做。</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202404/2024040109291315.png" /></p>
<p>以上是一个简单但完整的项目构成的过程,使用Cargo在hello world这样的项目中似乎有点大材小用,但Cargo是为了你构建大型项目准备的,你应该在学习Rust的初期,就直接使用Cargo,这对你以后管理rust项目非常重要。</p>
<p>当然,Cargo的指令远不止以上那些。</p>
<div class="jb51code"><pre class="brush:plain;"> 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. Default location is $HOME/.cargo/bin
uninstall Uninstall a Rust binary
</pre></div>
<p>如Cargo clean,将清除target文件夹。</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202404/2024040109291316.png" /></p>
<p class="maodian"></p><h2>总结</h2>
<p>通过以上的介绍,我们可以看到Cargo在Rust项目中的重要作用。它不仅简化了构建和分发过程,还提供了丰富的功能和灵活的扩展性。无论是初学者还是经验丰富的开发者,都可以通过Cargo来更加高效地创建、测试、文档化和分享自己的Rust项目。</p>
<p>到此这篇关于深入理解Rust中Cargo的使用的文章就介绍到这了,更多相关Rust Cargo内容请搜索琼殿技术社区以前的文章或继续浏览下面的相关文章希望大家以后多多支持琼殿技术社区!</p>
<div class="art_xg">
<b>您可能感兴趣的文章:</b><ul><li>Python安装jupyter notebook报pywinpty缺少Rust和Cargo问题</li><li>rust 如何使用 cargo-nextest 替代 cargo test</li><li>Rust中Cargo的使用详解</li><li>Rust中的Cargo构建、运行、调试</li><li>使用Cargo工具高效创建Rust项目</li><li>使用cargo install安装Rust二进制工具过程</li></ul>
</div>
</div>
<!--endmain-->
頁:
[1]