1小时放弃Rust(1): Hello-World

1. 目的

最近被公司项目的 crash 问题折磨, 一方面是crash位置不固定,难以复现;另一方面是代码、sdk、人员众多,我拿不到所有代码。排查效率非常低效,各种会议满天飞,问题实质毫无进展。希望在达到被辞退的节点前学会 Rust,写一些不可能存在内存踩踏问题的 sdk,拯救自己。

2. 安装 Rust

https://www.rust-lang.org/learn/get-started

bash 复制代码
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

3. 更新 Rust

bash 复制代码
rustup update

4. 创建 Hello-World 工程

bash 复制代码
mkdir -p ~/play/rust
cd $_
cargo new hello-world

5. 编译和运行

bash 复制代码
cd hello-world
cargo build
cargo run

6. 目录结构

刚执行 cargo new hello-world 时,文件和目录结构为:

bash 复制代码
➜  hello-world git:(main) ✗ tree -L 3
.
├── Cargo.lock
├── Cargo.toml
└── src
    └── main.rs

2 directories, 3 files

也可以 cargo clean 来恢复。

7. main.rs

rust 复制代码
fn main() {
    println!("Hello, world!");
}

解释:

  • fn: Rust 语言中的关键字。function 的意思
  • main(): 入口函数
  • println!: 宏. 暂时不理解,当做函数使用;会自动添加换行符的打印.

8. Cargo.toml

toml 复制代码
[package]
name = "hello-world"
version = "0.1.0"
edition = "2021"

[dependencies]

完全看不懂。 忽略。

9. Cargo run 在运行什么

bash 复制代码
cargo run
bash 复制代码
./target/debug/hello-world

10. 不用 cargo 呢?

hello.rs:

rust 复制代码
fn main()
{
    println!("Hello, Rust");
    println!("What a day!");
}

编译:

bash 复制代码
rustc hello.rs

运行:

bash 复制代码
./hello

结果:

bash 复制代码
Hello, Rust
What a day!

11. 总结

给出了 Rust 的安装,以及创建、运行、简单理解 hello-world 工程的步骤。

12. 参考

相关推荐
k4m7v2pz10 小时前
把 Bevy 0.14 游戏移植到 R36S 掌机:一场与“无窗口系统“的搏斗
linux·rust·bevy·r36s·rk3326·开源掌机
doiito11 小时前
【Agent Harness】流马(Gliding Horse)DeepSeek Responses API 接入介绍
rust·系统设计·ai agent
SomeB1oody17 小时前
【RustyML入门】2.0. 经典机器学习
开发语言·后端·机器学习·rust·教程
Embedded-Xin17 小时前
Rust学习——Cargo工具
linux·学习·架构·rust·嵌入式
龙仔72518 小时前
RustDesk 完整笔记
运维·笔记·rust·远程工具·rustdesk
An_s19 小时前
rust(pdfium)底层开发实现wasm包给vue3调用
开发语言·rust·wasm
对象存储与RustFS1 天前
为什么越来越多的企业选择RustFS作为对象存储?
后端·rust·开源
KryptonWright2 天前
让 AI Agent 只能提议、不能擅自执行:Rust 中 prepare-confirm-execute 权限模型的实现
rust
起司喵喵2 天前
推荐一款基于 Python 和 Rust 开发的跨平台 GUI 自动化库!
python·rust·自动化
花褪残红青杏小2 天前
Rust图像处理第24节- 噪声模型 + 中心极限定理:椒盐噪声 + CLT 可视化
rust·webassembly·图形学