Cargo

Cargo

cargo是rust的构建系统和包管理工具,在安装rust的时候就一并安装了cargo。

shell 复制代码
> cargo --version
cargo 1.78.0 (54d8815d0 2024-03-26)

使用cargo创建项目

shell 复制代码
cargo new hello_cargo

会生成

  • src 源码目录
  • Cargo.toml
  • Cargo.lock
  • .gitignore 仓库文件

Cargo.toml

Tom's Obvious, Minimal Language,是cargo的项目配置。

toml 复制代码
[package] # 包配置
name = "hello_cargo"                        # 包名
version = "0.1.0"                           # 包版本
authors = ["xiaolipro <[email protected]>"] # 作者
edition = "2021"                            # rust版本

[dependencies] # 第三方依赖项

target

build后下载的依赖库、生成的可执行文件都在这个目录

使用cargo clean命令可以删除

shell 复制代码
> cargo clean
     Removed 224 files, 16.1MiB total

编译

cargo check

cargo check命令用于代码检查,确保编译能够通过,但不会生成可执行文件。

因此cargo check比cargo build快得多,在编码阶段就可以频繁的使用,来检查代码、提高效率。

shell 复制代码
> cargo check     
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.02s

cargo build

cargo build命令就是在cargo check检查代码的基础上,多生成了可执行文件。

发布时,cargo build往往需要带上release参数,生成优化后的代码。

shell 复制代码
> cargo build --release
   Compiling hello_cargo v0.1.0 (C:\Antinew\我的\2024\rust\hello_cargo)
    Finished `release` profile [optimized] target(s) in 0.32s

可以看到,release模式,需要重新编译才能生成optimized代码,与之对应的,就是更长的等待时间

运行

cargo run

cargo run命令包含编译和执行。

shell 复制代码
> cargo run
   Compiling hello_cargo v0.1.0 (C:\Antinew\我的\2024\rust\hello_cargo)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.46s
     Running `target\debug\hello_cargo.exe`
Hello, world!

如果已经编译过了,且源码没有发生改变,那么会直接运行可执行文件。

相关推荐
Python私教7 小时前
征服Rust:从零到独立开发的实战进阶
服务器·开发语言·rust
Python私教12 小时前
Rust:重新定义系统编程的安全与效率边界
开发语言·安全·rust
明月看潮生19 小时前
青少年编程与数学 02-019 Rust 编程基础 12课题、所有权系统
开发语言·青少年编程·rust·编程与数学
景天科技苑21 小时前
【Rust trait特质】如何在Rust中使用trait特质,全面解析与应用实战
开发语言·后端·rust·trait·rust trait·rust特质
heroboyluck1 天前
rust 全栈应用框架dioxus server
rust·全栈·dioxus
蜗牛沐雨1 天前
Rust 中的 `PartialEq` 和 `Eq`:深入解析与应用
开发语言·后端·rust
Python私教1 天前
Rust快速入门:从零到实战指南
开发语言·后端·rust
明月看潮生2 天前
青少年编程与数学 02-019 Rust 编程基础 10课题、函数、闭包和迭代器
开发语言·青少年编程·rust·编程与数学
明月看潮生2 天前
青少年编程与数学 02-019 Rust 编程基础 09课题、流程控制
开发语言·算法·青少年编程·rust·编程与数学
一丝晨光2 天前
数值溢出保护?数值溢出应该是多少?Swift如何让整数计算溢出不抛出异常?类型最大值和最小值?
java·javascript·c++·rust·go·c·swift