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 <2357729423@qq.com>"] # 作者
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!

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

相关推荐
华科云商xiao徐6 小时前
Rust+Python双核爬虫:高并发采集与智能解析实战
数据库·python·rust
Saya6 小时前
Rust 命名模式速查表:15 秒看懂函数在做什么
rust
xuejianxinokok1 天前
解惑rust中的 Send/Sync(译)
后端·rust
得物技术2 天前
Rust 性能提升“最后一公里”:详解 Profiling 瓶颈定位与优化|得物技术
后端·rust
集成显卡2 天前
Rust 实战五 | 配置 Tauri 应用图标及解决 exe 被识别为威胁的问题
后端·rust
集成显卡3 天前
Rust 实战四 | Traui2+Vue3+Rspack 开发桌面应用:通配符掩码计算器
后端·程序员·rust
维维酱4 天前
四、VGA 文本缓冲区基础
rust
寻月隐君4 天前
Rust 实战:从零构建一个多线程 Web 服务器
后端·rust·github
受之以蒙4 天前
Rust & WebAssembly 性能调优指南:从毫秒级加速到KB级瘦身
笔记·rust·webassembly
Vallelonga4 天前
关于 Rust 异步(无栈协程)的相关疑问
开发语言·后端·rust