Skip to content

Repository files navigation

Rust 入门系列 - 配套代码示例

本仓库包含 Rust 入门系列文章 的所有配套代码示例。

📁 目录结构

rust-examples/
├── 01-ownership/       # 所有权 (4 个示例)
├── 02-borrow-checker/  # 借用检查器 (4 个示例)
├── 03-lifetime/        # 生命周期 (3 个示例)
├── 04-smart-pointers/  # 智能指针 (4 个示例)
├── 05-trait/           # Trait (3 个示例)
├── 06-generics-errors/ # 泛型与错误 (3 个示例)
├── 07-memory/          # 内存管理 (3 个示例)
├── 08-concurrency/     # 并发编程 (3 个示例)
├── 09-cargo/           # Cargo (1 个示例)
└── 10-ffi-macros/      # FFI 与宏 (2 个示例)

🚀 快速开始

# 运行特定示例
cd 01-ownership
cargo run --example move_example

# 运行所有示例
cargo run --all-examples

# 运行测试
cargo test --all

📝 示例列表

01-ownership (所有权)

  • move_example - 所有权转移
  • copy_example - Copy trait
  • clone_example - Clone trait
  • ownership_quiz - 小测验

02-borrow-checker (借用检查器)

  • immutable_borrow - 不可变借用
  • mutable_borrow - 可变借用
  • borrow_conflict - 借用冲突与修复
  • lifetime_basic - 生命周期基础

03-lifetime (生命周期)

  • lifetime_annot - 生命周期标注
  • lifetime_struct - 结构体生命周期
  • lifetime_elision - 省略规则

04-smart-pointers (智能指针)

  • box_example - Box 示例
  • rc_example - Rc 引用计数
  • arc_example - Arc 多线程
  • refcell_example - RefCell 内部可变性

05-trait (Trait)

  • trait_basic - Trait 基础
  • trait_object - Trait 对象
  • trait_impl - 常用 Trait 实现

06-generics-errors (泛型与错误)

  • generics_example - 泛型示例
  • result_example - Result 处理
  • option_example - Option 处理

07-memory (内存管理)

  • stack_heap - 栈与堆
  • drop_example - Drop trait
  • memory_layout - 内存布局

08-concurrency (并发编程)

  • thread_example - 线程示例
  • channel_example - 通道示例
  • async_example - 异步示例

09-cargo (Cargo)

  • workspace_demo - 工作空间演示

10-ffi-macros (FFI 与宏)

  • ffi_example - FFI 示例
  • macro_example - 宏示例

⚠️ 注意事项

  • 所有示例使用 Rust 2021 Edition
  • 需要 Rust 1.70+ 版本
  • 08-concurrency 需要网络连接(下载 tokio)

🔧 开发

# 格式化代码
cargo fmt --all

# 检查代码
cargo clippy --all

# 运行测试
cargo test --all

📄 许可证

MIT


配套 Rust 入门系列文章: 查看文章

About

Rust sample examples

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages