Learning Notes
Home
Home
  • Guide
    • Shortkeys
    • Commands
  • Cpp
    • Basic
    • Scope, Duration, and Linkage
    • Pointers(1)
    • Pointers(2)
  • Rust Learning
    • 1.Number Types
    • 2.Ownership
    • 3.Struct
    • 4.Enum
    • 5.Package-management
    • 6.Vector
    • 7.Error handle
    • 8.Trait 泛型
    • 9.Life time
    • 10.Test
    • 11.Minigrep
    • 12.Closure 闭包
    • 13.Smart pointer 智能指针
    • 14.Fearless concurrency 无畏并发
  • Quantum Computing
    • Basic computing and quantum computing Theory
    • Teleportation
    • QKD and QFT
    • QFE and Shor's algorithm
    • QAOA
    • Quantum algorithms
    • Week 11
  • Statical Machine Learning
    • Bayesian Linear Regression
    • Linear regression and logistic regresion
    • Bais
    • SVM, Kernel Methods
    • Precepction and Artificial Neural Network
    • Cross validation, experts learning, MAB
    • Bayesian inference
    • GMM-EM
    • final
  • Software Project Management
    • Lecture
    • Revision
  • AI-planning
    • Classical planning
    • MDP and reinforcement learning
    • Game theory and final recap

Minigrep

Souece code 源码

minigrep 是一个简单的命令行程序, 它从文件中搜索包含指定字符串的行, 并打印出包含该字符串的行

二进制程序关注点分离的指导性原则

  • 将程序拆分为main.rs和lib.rs, 将业务逻辑放入lib.rs
  • 当命令行解析逻辑较少时, 将它放入main.rs也行
  • 当命令行解析逻辑变复杂时, 需要将它从main.rs提取到lib.rs

经过上述拆分, 留在main的功能有:

  • 使用参数值调用命令行解析逻辑
  • 进行其他配置
  • 调用lib.rs中的run函数
  • 处理run函数可能出现的错误

测试驱动开发 TDD(Test-Driven Development)

  • 编写一个会失败的测试, 运行该测试, 确保它是按照预期的原因失败
  • 编写或修改刚好足够的代码, 使得新测试通过
  • 重构刚刚添加或修改的代码, 确保测试会始终通过
  • 返回步骤1, 继续

标准输出vs标准错误

  • 标准输出: stdout
    • println!
  • 标准错误: stderr
    • eprintln!
Last Updated:
Contributors: pingzhihe
Prev
10.Test
Next
12.Closure 闭包