rust-打包优化
【摘要】 rust-打包优化
rust-打包优化
目前我们打包以后项目大小为2.87 GB左右,这跟我们预期的过大,我们进行一下依赖优化以及打包优化处理
cargo build --release
依赖处理
诊断和解决方案
检查依赖树
# 查看完整的依赖树
cargo tree
# 查看特定依赖的大小
cargo tree | grep large_dependency_name
下载依赖分析工具
下载工具对于依赖进行观察
# 安装依赖分析工具
cargo install cargo-bloat
# 分析发布版依赖体积
cargo bloat --release --crates
[profile.release]
opt-level = "z" # 针对体积优化而非速度
lto = true # 启用链接时优化,消除未使用依赖
codegen-units = 1 # 单代码生成单元提高优化效率
panic = "abort" # 移除panic展开代码
strip = true # 自动剥离符号表
减小依赖文件大小
精简依赖功能
[dependencies]
# 不要使用 features = ["full"]
tokio = { version = "1.0", features = ["rt", "net"] }
# 而不是
# tokio = { version = "1.0", features = ["full"] }
# 只需要序列化功能时
serde = { version = "1.0", features = ["derive"] }
# 而不是
# serde = { version = "1.0" }
禁用特性
[dependencies]
libc = { version = "0.2", default-features = false }
通过default-features = false禁用默认特性,仅保留核心功能,这是控制依赖体积的首要原则。
更新依赖
// 更新所有依赖到最新兼容版本
cargo update
// 更新特定包到最新版本
cargo update -p package_name
重复依赖处理
现在我打包以后我的目标文件大小大致为2.7g,接下来我们就针对它进行优化
接下来我们可以针对cargo进行依赖的优化和处理,首先我们处理的就是重复依赖同时确保可以打包成功
# 查看依赖树
cargo tree
# 查看特定依赖的依赖
cargo tree -p serde
# 只显示直接依赖
cargo tree --depth 1
# 显示重复的依赖
cargo tree --duplicates
处理重复依赖
查看我们依赖冲突
cargo tree --duplicates
可以看出同样的依赖我有五个版本,接下来我们尝试固定版本在配置中
base64 v0.12.3
`-- mysql_common v0.24.1 (*)
base64 v0.13.1
`-- pem v0.8.3
`-- mysql v20.1.0 (*)
base64 v0.21.7
|-- rustls-pemfile v1.0.4
| `-- sqlx-core v0.7.4 (*)
|-- sqlx-mysql v0.7.4 (*)
|-- sqlx-mysql v0.7.4 (*)
`-- umya-spreadsheet v1.2.7 (*)
base64 v0.22.1
|-- bcrypt v0.15.1
| `-- nexus-rust-api v0.1.0 (D:\LTB\code\NexusRustApi)
|-- jsonwebtoken v9.3.1
| `-- nexus-rust-api v0.1.0 (D:\LTB\code\NexusRustApi)
`-- pem v3.0.6
`-- jsonwebtoken v9.3.1 (*)
固定版本大小
[dependencies]
base64 = { version = "0.22.1"} # base64编码
处理重复依赖futures
futures = "0.3" # 使用和管理异步操作
futures-util = "0.3" # 异步任务工具
futures-util 是 futures 的一部分,可以只保留 futures
对应的模块进行更改
use futures_util::stream::{self, StreamExt};
=>更改为
use futures::stream::{self, StreamExt};
cargo tree --duplicates重复依赖
我们发现有很多重复依赖项,这部分我们在 Cargo.toml 中来统一版本可以
actix-router v0.5.3
`-- actix-web-codegen v4.3.0 (proc-macro)
`-- actix-web v4.12.0
|-- actix-cors v0.6.5
| `-- nexusrustapi v0.1.0 (D:\LTB\code\NexusRustApi)
|-- actix-files v0.6.8
| `-- nexusrustapi v0.1.0 (D:\LTB\code\NexusRustApi)
|-- actix-multipart v0.4.0
| `-- nexusrustapi v0.1.0 (D:\LTB\code\NexusRustApi)
`-- nexusrustapi v0.1.0 (D:\LTB\code\NexusRustApi)
actix-router v0.5.3
`-- actix-web v4.12.0 (*)
ahash v0.7.8
`-- hashbrown v0.11.2
`-- lru v0.6.6
`-- mysql v20.1.0
`-- nexusrustapi v0.1.0 (D:\LTB\code\NexusRustApi)
ahash v0.8.12
|-- hashbrown v0.14.5
| |-- hashlink v0.8.4
| | `-- sqlx-core v0.7.4
| | |-- sqlx v0.7.4
| | | `-- nexusrustapi v0.1.0 (D:\LTB\code\NexusRustApi)
| | |-- sqlx-macros v0.7.4 (proc-macro)
| | | `-- sqlx v0.7.4 (*)
| | |-- sqlx-macros-core v0.7.4
| | | `-- sqlx-macros v0.7.4 (proc-macro) (*)
| | |-- sqlx-mysql v0.7.4
| | | `-- sqlx v0.7.4 (*)
| | `-- sqlx-mysql v0.7.4
| | `-- sqlx-macros-core v0.7.4 (*)
| `-- umya-spreadsheet v1.2.7
| `-- nexusrustapi v0.1.0 (D:\LTB\code\NexusRustApi)
|-- sqlx-core v0.7.4 (*)
`-- umya-spreadsheet v1.2.7 (*)
arrayvec v0.5.2
`-- lexical-core v0.7.6
`-- lexical v5.2.2
`-- mysql_common v0.24.1
`-- mysql v20.1.0 (*)
arrayvec v0.7.6
`-- rust_decimal v1.39.0
`-- mysql_common v0.24.1 (*)
base64 v0.12.3
`-- mysql_common v0.24.1 (*)
base64 v0.13.1
`-- pem v0.8.3
`-- mysql v20.1.0 (*)
base64 v0.21.7
|-- sqlx-mysql v0.7.4 (*)
`-- umya-spreadsheet v1.2.7 (*)
base64 v0.21.7
`-- sqlx-mysql v0.7.4 (*)
base64 v0.22.1
|-- actix-http v3.11.2
| |-- actix-files v0.6.8 (*)
| `-- actix-web v4.12.0 (*)
|-- bcrypt v0.15.1
| `-- nexusrustapi v0.1.0 (D:\LTB\code\NexusRustApi)
|-- jsonwebtoken v9.3.1
| `-- nexusrustapi v0.1.0 (D:\LTB\code\NexusRustApi)
`-- pem v3.0.6
`-- jsonwebtoken v9.3.1 (*)
bitflags v1.3.2
|-- lexical-core v0.7.6 (*)
|-- mysql_common v0.24.1 (*)
`-- png v0.17.16
`-- image v0.24.9
`-- umya-spreadsheet v1.2.7 (*)
bitflags v2.10.0
|-- actix-codec v0.5.2
| |-- actix-http v3.11.2 (*)
| `-- actix-web v4.12.0 (*)
|-- actix-files v0.6.8 (*)
|-- actix-http v3.11.2 (*)
|-- sqlx-mysql v0.7.4 (*)
`-- sqlx-mysql v0.7.4 (*)
block-buffer v0.7.3
`-- sha2 v0.8.2
`-- mysql_common v0.24.1 (*)
block-buffer v0.10.4
`-- digest v0.10.7
|-- hmac v0.12.1
| |-- hkdf v0.12.4
| | |-- sqlx-mysql v0.7.4 (*)
| | `-- sqlx-mysql v0.7.4 (*)
| |-- sqlx-mysql v0.7.4 (*)
| |-- sqlx-mysql v0.7.4 (*)
| `-- umya-spreadsheet v1.2.7 (*)
|-- md-5 v0.10.6
| `-- sqlx-mysql v0.7.4 (*)
|-- md-5 v0.10.6
| |-- sqlx-mysql v0.7.4 (*)
| `-- umya-spreadsheet v1.2.7 (*)
|-- rsa v0.9.9
| |-- sqlx-mysql v0.7.4 (*)
| `-- sqlx-mysql v0.7.4 (*)
|-- sha1 v0.10.6
| `-- sqlx-mysql v0.7.4 (*)
|-- sha1 v0.10.6
| |-- actix-http v3.11.2 (*)
| `-- sqlx-mysql v0.7.4 (*)
|-- sha2 v0.10.9
| |-- sqlx-core v0.7.4 (*)
| |-- sqlx-macros-core v0.7.4 (*)
| |-- sqlx-mysql v0.7.4 (*)
| |-- sqlx-mysql v0.7.4 (*)
| `-- umya-spreadsheet v1.2.7 (*)
| [build-dependencies]
| `-- pest_meta v2.8.4
| `-- pest_generator v2.8.4
| `-- pest_derive v2.8.4 (proc-macro)
| `-- html_parser v0.7.0
| `-- umya-spreadsheet v1.2.7 (*)
|-- signature v2.2.0
| `-- rsa v0.9.9 (*)
|-- sqlx-mysql v0.7.4 (*)
`-- sqlx-mysql v0.7.4 (*)
block-padding v0.1.5
`-- block-buffer v0.7.3 (*)
block-padding v0.3.3
`-- inout v0.1.4
`-- cipher v0.4.4
|-- aes v0.8.4
| `-- umya-spreadsheet v1.2.7 (*)
|-- blowfish v0.9.1
| `-- bcrypt v0.15.1 (*)
`-- cbc v0.1.2
`-- umya-spreadsheet v1.2.7 (*)
byteorder v1.5.0
|-- block-buffer v0.7.3 (*)
|-- blowfish v0.9.1 (*)
|-- calamine v0.21.2
| `-- nexusrustapi v0.1.0 (D:\LTB\code\NexusRustApi)
|-- cfb v0.9.0
| `-- umya-spreadsheet v1.2.7 (*)
|-- image v0.24.9 (*)
|-- mysql_common v0.24.1 (*)
|-- sqlx-core v0.7.4 (*)
|-- sqlx-mysql v0.7.4 (*)
|-- umya-spreadsheet v1.2.7 (*)
`-- zip v0.6.6
|-- calamine v0.21.2 (*)
`-- umya-spreadsheet v1.2.7 (*)
byteorder v1.5.0
`-- sqlx-mysql v0.7.4 (*)
bytes v0.5.6
`-- mysql_common v0.24.1 (*)
bytes v1.11.0
|-- actix-codec v0.5.2 (*)
|-- actix-files v0.6.8 (*)
|-- actix-http v3.11.2 (*)
|-- actix-multipart v0.4.0 (*)
|-- actix-web v4.12.0 (*)
|-- bytestring v1.5.0
| |-- actix-http v3.11.2 (*)
| |-- actix-router v0.5.3 (*)
| |-- actix-router v0.5.3 (*)
| `-- actix-web v4.12.0 (*)
|-- h2 v0.3.27
| `-- actix-http v3.11.2 (*)
|-- http v0.2.12
| |-- actix-http v3.11.2 (*)
| |-- actix-router v0.5.3 (*)
| `-- h2 v0.3.27 (*)
|-- sqlx-core v0.7.4 (*)
|-- sqlx-mysql v0.7.4 (*)
|-- sqlx-mysql v0.7.4 (*)
|-- tokio v1.48.0
| |-- actix-codec v0.5.2 (*)
| |-- actix-http v3.11.2 (*)
| |-- actix-rt v2.11.0
| | |-- actix-http v3.11.2 (*)
| | |-- actix-server v2.6.0
| | | `-- actix-web v4.12.0 (*)
| | `-- actix-web v4.12.0 (*)
| |-- actix-server v2.6.0 (*)
| |-- h2 v0.3.27 (*)
| |-- nexusrustapi v0.1.0 (D:\LTB\code\NexusRustApi)
| |-- sqlx-core v0.7.4 (*)
| |-- tokio-stream v0.1.17
| | `-- sqlx-core v0.7.4 (*)
| `-- tokio-util v0.7.17
| |-- actix-codec v0.5.2 (*)
| |-- actix-http v3.11.2 (*)
| `-- h2 v0.3.27 (*)
|-- tokio v1.48.0
| `-- sqlx-macros-core v0.7.4 (*)
`-- tokio-util v0.7.17 (*)
derive_more v0.99.20 (proc-macro)
|-- actix-cors v0.6.5 (*)
`-- actix-multipart v0.4.0 (*)
derive_more v2.0.1
|-- actix-files v0.6.8 (*)
|-- actix-http v3.11.2 (*)
`-- actix-web v4.12.0 (*)
digest v0.8.1
`-- sha2 v0.8.2 (*)
digest v0.10.7 (*)
form_urlencoded v1.2.2
`-- url v2.5.7
`-- sqlx-macros-core v0.7.4 (*)
form_urlencoded v1.2.2
|-- serde_urlencoded v0.7.1
| `-- actix-web v4.12.0 (*)
`-- url v2.5.7
|-- actix-web v4.12.0 (*)
|-- mysql v20.1.0 (*)
`-- sqlx-core v0.7.4 (*)
futures-sink v0.3.31
`-- futures-util v0.3.31
`-- sqlx-mysql v0.7.4 (*)
futures-sink v0.3.31
|-- actix-codec v0.5.2 (*)
|-- futures v0.3.31
| `-- nexusrustapi v0.1.0 (D:\LTB\code\NexusRustApi)
|-- futures-channel v0.3.31
| |-- futures v0.3.31 (*)
| |-- futures-util v0.3.31
| | |-- actix-cors v0.6.5 (*)
| | |-- actix-server v2.6.0 (*)
| | |-- actix-web v4.12.0 (*)
| | |-- futures v0.3.31 (*)
| | |-- futures-executor v0.3.31
| | | `-- futures v0.3.31 (*)
| | |-- h2 v0.3.27 (*)
| | |-- sqlx-core v0.7.4 (*)
| | `-- sqlx-mysql v0.7.4 (*)
| |-- sqlx-core v0.7.4 (*)
| |-- sqlx-mysql v0.7.4 (*)
| `-- sqlx-mysql v0.7.4 (*)
|-- futures-util v0.3.31 (*)
|-- h2 v0.3.27 (*)
|-- local-channel v0.1.5
| `-- actix-http v3.11.2 (*)
`-- tokio-util v0.7.17 (*)
futures-util v0.3.31 (*)
futures-util v0.3.31 (*)
generic-array v0.12.4
|-- block-buffer v0.7.3 (*)
`-- digest v0.8.1 (*)
generic-array v0.14.7
|-- block-buffer v0.10.4 (*)
|-- block-padding v0.3.3 (*)
|-- crypto-common v0.1.7
| |-- cipher v0.4.4 (*)
| `-- digest v0.10.7 (*)
|-- inout v0.1.4 (*)
|-- sqlx-mysql v0.7.4 (*)
`-- sqlx-mysql v0.7.4 (*)
getrandom v0.1.16
|-- rand v0.7.3
| `-- mysql_common v0.24.1 (*)
`-- rand_core v0.5.1
|-- rand v0.7.3 (*)
`-- rand_chacha v0.2.2
`-- rand v0.7.3 (*)
getrandom v0.2.16
|-- ahash v0.7.8 (*)
|-- bcrypt v0.15.1 (*)
|-- rand_core v0.6.4
| |-- rand v0.8.5
| | |-- sqlx-mysql v0.7.4 (*)
| | `-- twox-hash v1.6.3
| | |-- mysql v20.1.0 (*)
| | `-- mysql_common v0.24.1 (*)
| |-- rand v0.8.5
| | |-- num-bigint-dig v0.8.6
| | | `-- rsa v0.9.9 (*)
| | `-- sqlx-mysql v0.7.4 (*)
| |-- rand_chacha v0.3.1
| | |-- rand v0.8.5 (*)
| | `-- rand v0.8.5 (*)
| |-- rsa v0.9.9 (*)
| `-- signature v2.2.0 (*)
|-- ring v0.17.14
| `-- jsonwebtoken v9.3.1 (*)
`-- umya-spreadsheet v1.2.7 (*)
getrandom v0.3.4
|-- ahash v0.8.12 (*)
|-- jobserver v0.1.34
| `-- cc v1.2.47
| [build-dependencies]
| |-- libz-sys v1.1.23
| | `-- flate2 v1.1.5
| | |-- actix-http v3.11.2 (*)
| | |-- mysql_common v0.24.1 (*)
| | |-- png v0.17.16 (*)
| | |-- tiff v0.9.1
| | | `-- image v0.24.9 (*)
| | `-- zip v0.6.6 (*)
| |-- ring v0.17.14 (*)
| `-- zstd-sys v2.0.16+zstd.1.5.7
| `-- zstd-safe v7.2.4
| `-- zstd v0.13.3
| `-- actix-http v3.11.2 (*)
|-- rand_core v0.9.3
| |-- rand v0.9.2
| | `-- actix-http v3.11.2 (*)
| `-- rand_chacha v0.9.0
| `-- rand v0.9.2 (*)
|-- tempfile v3.23.0
| `-- sqlx-macros-core v0.7.4 (*)
`-- uuid v1.18.1
|-- cfb v0.9.0 (*)
`-- nexusrustapi v0.1.0 (D:\LTB\code\NexusRustApi)
hashbrown v0.11.2 (*)
hashbrown v0.14.5 (*)
hashbrown v0.16.1
`-- indexmap v2.12.1
|-- h2 v0.3.27 (*)
`-- sqlx-core v0.7.4 (*)
idna v1.1.0
`-- url v2.5.7 (*)
idna v1.1.0
`-- url v2.5.7 (*)
log v0.4.28
`-- sqlx-mysql v0.7.4 (*)
log v0.4.28
|-- actix-cors v0.6.5 (*)
|-- actix-files v0.6.8 (*)
|-- actix-multipart v0.4.0 (*)
|-- actix-web v4.12.0 (*)
|-- calamine v0.21.2 (*)
|-- env_logger v0.9.3
| `-- nexusrustapi v0.1.0 (D:\LTB\code\NexusRustApi)
|-- mio v1.1.0
| |-- actix-server v2.6.0 (*)
| `-- tokio v1.48.0 (*)
|-- nexusrustapi v0.1.0 (D:\LTB\code\NexusRustApi)
|-- sqlx-core v0.7.4 (*)
|-- sqlx-mysql v0.7.4 (*)
`-- tracing v0.1.41
|-- actix-codec v0.5.2 (*)
|-- actix-http v3.11.2 (*)
|-- actix-router v0.5.3 (*)
|-- actix-router v0.5.3 (*)
|-- actix-server v2.6.0 (*)
|-- actix-web v4.12.0 (*)
|-- h2 v0.3.27 (*)
|-- sqlx-core v0.7.4 (*)
|-- sqlx-mysql v0.7.4 (*)
`-- sqlx-mysql v0.7.4 (*)
md-5 v0.10.6 (*)
md-5 v0.10.6 (*)
memchr v2.7.6
|-- futures-util v0.3.31 (*)
`-- sqlx-mysql v0.7.4 (*)
memchr v2.7.6
|-- actix-codec v0.5.2 (*)
|-- aho-corasick v1.1.4
| |-- regex v1.12.2
| | |-- actix-router v0.5.3 (*)
| | |-- actix-web v4.12.0 (*)
| | |-- env_logger v0.9.3 (*)
| | |-- mysql_common v0.24.1 (*)
| | |-- pem v0.8.3 (*)
| | `-- umya-spreadsheet v1.2.7 (*)
| `-- regex-automata v0.4.13
| |-- fancy-regex v0.13.0
| | `-- umya-spreadsheet v1.2.7 (*)
| `-- regex v1.12.2 (*)
|-- csv-core v0.1.13
| `-- csv v1.4.0
| `-- nexusrustapi v0.1.0 (D:\LTB\code\NexusRustApi)
|-- futures-util v0.3.31 (*)
|-- nom v7.1.3
| `-- sqlformat v0.2.6
| `-- sqlx-core v0.7.4 (*)
|-- pest v2.8.4
| |-- html_parser v0.7.0 (*)
| |-- pest_derive v2.8.4 (proc-macro) (*)
| |-- pest_generator v2.8.4 (*)
| `-- pest_meta v2.8.4 (*)
|-- quick-xml v0.28.2
| `-- calamine v0.21.2 (*)
|-- quick-xml v0.31.0
| `-- umya-spreadsheet v1.2.7 (*)
|-- regex v1.12.2 (*)
|-- regex-automata v0.4.13 (*)
|-- serde_json v1.0.145
| |-- actix-web v4.12.0 (*)
| |-- html_parser v0.7.0 (*)
| |-- jsonwebtoken v9.3.1 (*)
| |-- mysql v20.1.0 (*)
| |-- mysql_common v0.24.1 (*)
| |-- nexusrustapi v0.1.0 (D:\LTB\code\NexusRustApi)
| |-- sqlx-core v0.7.4 (*)
| `-- sqlx-macros-core v0.7.4 (*)
|-- sqlx-core v0.7.4 (*)
|-- sqlx-mysql v0.7.4 (*)
`-- twoway v0.2.2
`-- actix-multipart v0.4.0 (*)
mio v1.1.0 (*)
mio v1.1.0
`-- tokio v1.48.0 (*)
num-bigint v0.2.6
|-- bigdecimal v0.1.2
| `-- mysql_common v0.24.1 (*)
`-- mysql_common v0.24.1 (*)
num-bigint v0.4.6
`-- simple_asn1 v0.6.3
`-- jsonwebtoken v9.3.1 (*)
num-integer v0.1.46
|-- bigdecimal v0.1.2 (*)
|-- num-bigint v0.2.6 (*)
`-- num-bigint v0.4.6 (*)
num-integer v0.1.46
|-- num-bigint-dig v0.8.6 (*)
|-- num-iter v0.1.45
| `-- num-bigint-dig v0.8.6 (*)
`-- rsa v0.9.9 (*)
num-traits v0.2.19
|-- atoi v2.0.0
| |-- sqlx-core v0.7.4 (*)
| |-- sqlx-mysql v0.7.4 (*)
| `-- sqlx-mysql v0.7.4 (*)
|-- bigdecimal v0.1.2 (*)
|-- chrono v0.4.42
| |-- mysql_common v0.24.1 (*)
| |-- nexusrustapi v0.1.0 (D:\LTB\code\NexusRustApi)
| `-- umya-spreadsheet v1.2.7 (*)
|-- image v0.24.9 (*)
|-- mysql_common v0.24.1 (*)
|-- num-bigint v0.2.6 (*)
|-- num-bigint v0.4.6 (*)
|-- num-integer v0.1.46 (*)
|-- rust_decimal v1.39.0 (*)
`-- simple_asn1 v0.6.3 (*)
num-traits v0.2.19
|-- num-bigint-dig v0.8.6 (*)
|-- num-integer v0.1.46 (*)
|-- num-iter v0.1.45 (*)
`-- rsa v0.9.9 (*)
pem v0.8.3 (*)
pem v3.0.6 (*)
quick-xml v0.28.2 (*)
quick-xml v0.31.0 (*)
rand v0.7.3 (*)
rand v0.8.5 (*)
rand v0.8.5 (*)
rand v0.9.2 (*)
rand_chacha v0.2.2 (*)
rand_chacha v0.3.1 (*)
rand_chacha v0.9.0 (*)
rand_core v0.5.1 (*)
rand_core v0.6.4 (*)
rand_core v0.9.3 (*)
sha1 v0.6.1
`-- mysql_common v0.24.1 (*)
sha1 v0.10.6 (*)
sha1 v0.10.6 (*)
sha2 v0.8.2 (*)
sha2 v0.10.9 (*)
socket2 v0.3.19
`-- mysql v20.1.0 (*)
socket2 v0.5.10
`-- actix-server v2.6.0 (*)
socket2 v0.6.1
|-- actix-web v4.12.0 (*)
|-- tokio v1.48.0 (*)
`-- tokio v1.48.0 (*)
sqlx-mysql v0.7.4 (*)
sqlx-mysql v0.7.4 (*)
standback v0.2.17
`-- time-macros-impl v0.1.2 (proc-macro)
`-- time-macros v0.1.1
`-- time v0.2.27
`-- mysql_common v0.24.1 (*)
standback v0.2.17
`-- time v0.2.27 (*)
syn v1.0.109
|-- derive_utils v0.11.2
| `-- io-enum v0.2.6 (proc-macro)
| `-- mysql v20.1.0 (*)
|-- io-enum v0.2.6 (proc-macro) (*)
|-- sqlx-macros v0.7.4 (proc-macro) (*)
|-- sqlx-macros-core v0.7.4 (*)
`-- time-macros-impl v0.1.2 (proc-macro) (*)
syn v2.0.111
|-- actix-macros v0.2.4 (proc-macro)
| `-- actix-web v4.12.0 (*)
|-- actix-web-codegen v4.3.0 (proc-macro) (*)
|-- derive_more v0.99.20 (proc-macro) (*)
|-- derive_more-impl v2.0.1 (proc-macro)
| `-- derive_more v2.0.1 (*)
|-- displaydoc v0.2.5 (proc-macro)
| |-- icu_collections v2.1.1
| | |-- icu_normalizer v2.1.1
| | | `-- idna_adapter v1.2.1
| | | |-- idna v1.1.0 (*)
| | | `-- idna v1.1.0 (*)
| | `-- icu_properties v2.1.1
| | `-- idna_adapter v1.2.1 (*)
| |-- icu_locale_core v2.1.1
| | |-- icu_properties v2.1.1 (*)
| | `-- icu_provider v2.1.1
| | |-- icu_normalizer v2.1.1 (*)
| | `-- icu_properties v2.1.1 (*)
| |-- icu_provider v2.1.1 (*)
| |-- tinystr v0.8.2
| | `-- icu_locale_core v2.1.1 (*)
| `-- zerotrie v0.2.3
| |-- icu_properties v2.1.1 (*)
| `-- icu_provider v2.1.1 (*)
|-- futures-macro v0.3.31 (proc-macro)
| `-- futures-util v0.3.31 (*)
|-- pest_generator v2.8.4 (*)
|-- serde_derive v1.0.228 (proc-macro)
| |-- html_parser v0.7.0 (*)
| `-- serde v1.0.228
| |-- actix-router v0.5.3 (*)
| |-- actix-router v0.5.3 (*)
| |-- actix-web v4.12.0 (*)
| |-- bigdecimal v0.1.2 (*)
| |-- calamine v0.21.2 (*)
| |-- chrono v0.4.42 (*)
| |-- either v1.15.0
| | |-- rayon v1.11.0
| | | `-- jpeg-decoder v0.3.2
| | | |-- image v0.24.9 (*)
| | | `-- tiff v0.9.1 (*)
| | |-- sqlx-core v0.7.4 (*)
| | |-- sqlx-macros-core v0.7.4 (*)
| | |-- sqlx-mysql v0.7.4 (*)
| | `-- sqlx-mysql v0.7.4 (*)
| |-- html_parser v0.7.0 (*)
| |-- jsonwebtoken v9.3.1 (*)
| |-- mysql v20.1.0 (*)
| |-- mysql_common v0.24.1 (*)
| |-- nexusrustapi v0.1.0 (D:\LTB\code\NexusRustApi)
| |-- quick-xml v0.31.0 (*)
| |-- rust_decimal v1.39.0 (*)
| |-- serde_urlencoded v0.7.1 (*)
| |-- sqlx-core v0.7.4 (*)
| |-- sqlx-macros-core v0.7.4 (*)
| |-- sqlx-mysql v0.7.4 (*)
| |-- sqlx-mysql v0.7.4 (*)
| `-- url v2.5.7 (*)
|-- synstructure v0.13.2
| |-- yoke-derive v0.8.1 (proc-macro)
| | `-- yoke v0.8.1
| | |-- icu_collections v2.1.1 (*)
| | |-- icu_provider v2.1.1 (*)
| | |-- zerotrie v0.2.3 (*)
| | `-- zerovec v0.11.5
| | |-- icu_collections v2.1.1 (*)
| | |-- icu_locale_core v2.1.1 (*)
| | |-- icu_normalizer v2.1.1 (*)
| | |-- icu_properties v2.1.1 (*)
| | |-- icu_provider v2.1.1 (*)
| | |-- potential_utf v0.1.4
| | | `-- icu_collections v2.1.1 (*)
| | `-- tinystr v0.8.2 (*)
| `-- zerofrom-derive v0.1.6 (proc-macro)
| `-- zerofrom v0.1.6
| |-- icu_collections v2.1.1 (*)
| |-- icu_provider v2.1.1 (*)
| |-- yoke v0.8.1 (*)
| |-- zerotrie v0.2.3 (*)
| `-- zerovec v0.11.5 (*)
|-- thiserror-impl v1.0.69 (proc-macro)
| `-- thiserror v1.0.69
| |-- html_parser v0.7.0 (*)
| |-- sqlx-core v0.7.4 (*)
| |-- sqlx-mysql v0.7.4 (*)
| `-- sqlx-mysql v0.7.4 (*)
|-- thiserror-impl v2.0.17 (proc-macro)
| `-- thiserror v2.0.17
| `-- simple_asn1 v0.6.3 (*)
|-- tracing-attributes v0.1.30 (proc-macro)
| `-- tracing v0.1.41 (*)
|-- yoke-derive v0.8.1 (proc-macro) (*)
|-- zerocopy-derive v0.8.30 (proc-macro)
| `-- zerocopy v0.8.30
| |-- ahash v0.8.12 (*)
| |-- half v2.7.1
| | `-- exr v1.74.0
| | `-- image v0.24.9 (*)
| `-- ppv-lite86 v0.2.21
| |-- rand_chacha v0.2.2 (*)
| |-- rand_chacha v0.3.1 (*)
| `-- rand_chacha v0.9.0 (*)
|-- zerofrom-derive v0.1.6 (proc-macro) (*)
`-- zerovec-derive v0.11.2 (proc-macro)
`-- zerovec v0.11.5 (*)
thiserror v1.0.69 (*)
thiserror v2.0.17 (*)
thiserror-impl v1.0.69 (proc-macro) (*)
thiserror-impl v2.0.17 (proc-macro) (*)
time v0.2.27 (*)
time v0.3.44
|-- actix-web v4.12.0 (*)
|-- cookie v0.16.2
| `-- actix-web v4.12.0 (*)
`-- simple_asn1 v0.6.3 (*)
time-macros v0.1.1 (*)
time-macros v0.2.24 (proc-macro)
`-- time v0.3.44 (*)
tokio v1.48.0 (*)
tokio v1.48.0 (*)
url v2.5.7 (*)
url v2.5.7 (*)
uuid v0.8.2
`-- mysql_common v0.24.1 (*)
uuid v1.18.1 (*)
windows-sys v0.52.0
`-- socket2 v0.5.10 (*)
windows-sys v0.60.2
`-- socket2 v0.6.1 (*)
windows-sys v0.61.2
|-- mio v1.1.0 (*)
|-- schannel v0.1.28
| `-- native-tls v0.2.14
| |-- mysql v20.1.0 (*)
| `-- sqlx-core v0.7.4 (*)
|-- tokio v1.48.0 (*)
`-- winapi-util v0.1.11
`-- termcolor v1.4.1
`-- env_logger v0.9.3 (*)
windows-sys v0.61.2
|-- mio v1.1.0 (*)
|-- tempfile v3.23.0 (*)
`-- tokio v1.48.0 (*)
windows-targets v0.52.6
`-- windows-sys v0.52.0 (*)
windows-targets v0.53.5
`-- windows-sys v0.60.2 (*)
windows_x86_64_msvc v0.52.6
`-- windows-targets v0.52.6 (*)
windows_x86_64_msvc v0.53.1
`-- windows-targets v0.53.5 (*)
移除futures
futures = { version = "0.3", default-features = false }
EXCEL表格依赖移除
之前我们对比了两种对于表格处理的依赖,由于项目过度庞大,所以我们移除掉其中的umya-spreadsheet,移除了 umya-spreadsheet,因为 calamine 更轻量且功能足够
umya-spreadsheet = "1.1"
之前的代码
// umya-spreadsheet 写法
use std::io::Cursor;
use umya_spreadsheet::*;
pub async fn export_users_template() -> HttpResponse {
// 创建一个带默认 Sheet1 的工作簿
let mut book = new_file();
let sheet = book.get_sheet_by_name_mut("Sheet1").unwrap();
// 表头
sheet.get_cell_mut((1, 1)).set_value("用户名");
sheet.get_cell_mut((2, 1)).set_value("姓名");
sheet.get_cell_mut((3, 1)).set_value("年龄");
sheet.get_cell_mut((4, 1)).set_value("电话");
// 写到内存
let mut buffer = Cursor::new(Vec::new());
writer::xlsx::write_writer(&book, &mut buffer).unwrap();
let bytes = buffer.into_inner();
HttpResponse::Ok()
.append_header(("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
.append_header(("Content-Disposition", "attachment; filename=\"users_template.xlsx\""))
.append_header(("Access-Control-Expose-Headers", "Content-Disposition, Content-Type"))
.body(bytes)
}
移除mysql依赖
sqlx和mysql重复,我们移除mysql依赖
旧的写法
// 连接池实现
use mysql::*;
use dotenv::dotenv;
use std::env;
// 创建数据库连接
pub fn create_pool() -> mysql::Conn {
dotenv().ok();
let url = env::var("DATABASE_URL")
.expect("DATABASE_URL environment variable not set");
let opts = Opts::from_url(&url)
.expect("Failed to parse database URL");
Conn::new(opts)
.expect("Failed to connect to MySQL database")
}
新的写法
// sqlx方式
use sqlx::{mysql::MySqlPoolOptions, MySqlPool};
use dotenv::dotenv;
use std::env;
// 创建数据库连接池
pub async fn create_pool() -> Result<MySqlPool, sqlx::Error> {
dotenv().ok();
let database_url = env::var("DATABASE_URL")
.expect("DATABASE_URL environment variable not set");
// 创建连接池
let pool = MySqlPoolOptions::new()
.max_connections(10) // 设置最大连接数
.connect(&database_url)
.await?;
Ok(pool)
打包体积处理
目前我们打包以后的代码为1.8g,我们优化一下
使用 UPX 压缩(最直接有效)
winget install upx.upx
// 重新打包
cargo build --release
//压缩二进制源码
# 压缩 release 模式的二进制文件
upx -9 target/release/your_app_name.exe
# 压缩并保留调试信息(如果需要)
upx --debug target/release/your_app_name.exe
使用动态链接减少库
# 设置 RUSTFLAGS 使用动态链接
export RUSTFLAGS="-C link-arg=-Wl,-rpath, -C link-arg=-lssl -C link-arg=-lcrypto"
# 重新编译
cargo build --release
移除未使用的依赖
cargo install cargo-machete
cargo machete
语法兼容处理
这部分主要针对于我们的项目进行rust打包过程的处理
之前我们使用的版本一直是edition = "2024"但是rust最新稳定版本只有2021
Cargo.toml
[package]
name = "nexusrustapi"
version = "0.1.0"
edition = "2021"
打包处理
语法兼容优化
之前我们的语法都是使用的2024的模块进行的,接下来我们降级到2021,并且兼容2021的写法
安装必须包
打包处理
这里打包的时候一直提示我说
error: failed to run custom build command for `openssl-sys v0.9.111`
// 更新包列表并安装必要的包
sudo apt-get update
sudo apt-get install pkg-config libssl-dev build-essential
// 验证安装
pkg-config --modversion openssl
openssl version
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)