daily notes[44]
Rust入门:"你好,世界"示例 本文展示了Rust语言的第一个程序示例。与其他语言不同,Rust建议为每个项目创建独立目录。示例中: 创建项目目录 编写main.rs文件,包含简单的打印语句 使用rustc编译并运行程序 程序输出传统的"你好,世界!"问候。文章还提供了Rust官方网站作为参考资源。
·
文章目录
基础
- hello,world is the first example in learning almost any programming language,and Rust is no exception, However, it differs in that a project written in Rust should ideally have its own directory.
$ mkdir helloWorld
$ cd helloWorld
we create a new file named main.rs(rs is the file extension for Rust source code files) , and add the following code to it.
fn main() {
println!("你好,世界!");
}
$ rustc main.rs
$ ./main
你好,世界!
- as a Rust programmer, you should be familar with the cargo, the Rust toolkit. cargo is reponsible for managing packages and building project,especially when you are working on large-scale coding tasks .
although thehello,world
project does not seem complex, we can still use cargo to manage it to form good habits.
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>
there are two files and one directory,The directory is named src and is created by Cargo by default. It contains only the source file main.rs.
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
运行项目
references
- https://www.rust-lang.org/learn/
- deepseek
更多推荐
所有评论(0)