daily notes[44]

发布于:2025-09-15 ⋅ 阅读:(33) ⋅ 点赞:(0)

文章目录

基础

  1. hello,world是几乎所有编程语言的第一例子,rust也不例外。但和其它语言不一样,Rust的源码最好拥有自己的项目目录。
$ mkdir helloWorld
$ cd helloWorld

源代码文件名为main.rs,内容如下

fn main() {
    println!("你好,世界!");
}
$ rustc main.rs
$ ./main
你好,世界!
  1. 作为rust程序员,cargo是必须熟悉的工具组,它是包管理器和编译系统,尤其是在编程任务量大的情况下,它能有效提高工作效率。
    虽然hello,world很简单,但为了养成良好习惯,我们也可以使用cargo来管理这个项目。
PS E:\learn\rust> cargo new hello
    Creating binary (application) `hello` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
PS E:\learn\rust> cd hello
PS E:\learn\rust\hello>

hello目录下有两个文件和一个目录,这个名为src的目录下唯一的源代码文件main.rs,这是cargo默认为我们创建好的。

PS E:\learn\rust\hello> ls

    Directory: E:\learn\rust\hello

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           2025/9/14     7:41                src
-a---           2025/9/14     7:41              8 .gitignore
-a---           2025/9/14     7:41             76 Cargo.toml

PS E:\learn\rust\hello>
PS E:\learn\rust\hello> ls src

    Directory: E:\learn\rust\hello\src

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---           2025/9/14     7:41             45 main.rs

.gitignore 文件的功能是:在项目中创建或修改的文件和目录,只要符合 .gitignore 文件中定义的规则,Git 就会完全无视它们。
Cargo.toml 是 Rust 项目的清单文件(Manifest File)。它定义了 Rust 包(在 Rust 中称为 Crate)的所有元信息、依赖项、构建配置等。

  • [package] 表 - 定义包信息
  • [dependencies] 表 - 定义依赖项
  • [dev-dependencies] 表 - 定义开发依赖
  • [build-dependencies] 表 - 定义构建依赖
  • [features] 表 - 定义条件编译特性
  • [lib] 和 [[bin]] 表 - 定义库和二进制目标
  • [profile] 表 - 自定义编译配置

下面是一个完整的例子

[package]
name = "web-scraper"
version = "0.2.1"
edition = "2021"
authors = ["Jane Developer <jane@example.com>", "John Contributor <john@contributor.com>"]
description = "A fast and efficient web scraping tool that extracts data from websites"
license = "MIT OR Apache-2.0"
readme = "README.md"
repository = "https://github.com/janedev/web-scraper"
homepage = "https://github.com/janedev/web-scraper"
documentation = "https://docs.rs/web-scraper"
keywords = ["scraping", "web", "html", "async", "cli"]
categories = ["command-line-utilities", "web-programming", "api-tools"]
default-run = "web-scraper"  # 指定默认运行的二进制名称

# 项目的 Rust 版本要求
rust-version = "1.70"

[dependencies]
# 异步 HTTP 客户端
reqwest = { version = "0.11", features = ["json", "stream"] }
# HTML 解析库
scraper = "0.16"
# 异步运行时
tokio = { version = "1.32", features = ["full"] }
# 命令行参数解析
clap = { version = "4.4", features = ["derive"] }
# 序列化/反序列化
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
# 错误处理
anyhow = "1.0"
thiserror = "1.0"
# 日志记录
log = "0.4"
env_logger = "0.10"
# 正则表达式(可选功能)
regex = { version = "1.10", optional = true }
# 进度条显示
indicatif = { version = "0.17", optional = true }

[dev-dependencies]
# 测试框架
tokio-test = "0.4"
# 测试断言
assertables = "6.0"
# 模拟 HTTP 响应
wiremock = "0.5"
# 临时文件处理
tempfile = "3.8"

[build-dependencies]
# 构建时生成版本信息
vergen = "8.0"

[features]
default = ["regex", "progress-bar"]
# 正则表达式支持
regex = ["dep:regex"]  # 显式启用 regex 依赖
# 进度条支持
progress-bar = ["dep:indicatif"]
# 所有功能
full = ["regex", "progress-bar"]
# 最小化功能(无可选依赖)
minimal = []

# 为特定平台设置依赖或特性
[target.'cfg(unix)'.dependencies]
nix = "0.27"

[[bin]]
name = "web-scraper"
path = "src/main.rs"
# 设置二进制文件的元数据(用于包管理器)
[package.metadata.deb]
maintainer-scripts = "debian"

# 性能优化配置
[profile.release]
lto = true
codegen-units = 1
panic = "abort"

[profile.bench]
debug = true

# 工作区配置(如果这是一个工作区根目录)
[workspace]
members = [
    "crates/*",
    "examples/*"
]
resolver = "2"

# 包发布设置
[package.metadata.docs.rs]
all-features = true
targets = ["x86_64-unknown-linux-gnu"]

# 自定义工具配置
[package.metadata.clippy]
all-targets = false

# 别名设置(Cargo 命令别名)
[alias]
b = "build"
t = "test"
rr = "run --release"
ci-test = "test --all-features --no-fail-fast"

命令cargo build编译项目,命令cargo run 运行项目


“Ciao, mondo” è il primo esempio in quasi tutti i linguaggi di programmazione.Ma a differenza di altri linguaggi, il codice sorgente di Rust funziona meglio quando ha la propria directory del progetto.

references

  1. https://www.rust-lang.org/learn/
  2. deepseek