- Hands-On Data Structures and Algorithms with Rust
- Claus Matzinger
- 131字
- 2021-07-02 14:11:43
Shadowing
Instead of changing variable properties, it's often more readable to overwrite a variable with a different value (for example, a changed copy of the original). This technique is called shadowing.
Typically, this is used to reuse a variable name, even though the actual value has changed, to work in the current situation. This snippet sanitizes String and, by using the same name throughout the function, it's always clear that it's the input parameter that is changed:
fn sanitize(s: String) -> String {
let s = s.trim();
let s = s.replace(" ", "_");
s
}
While this is akin to changing the value of a variable, shadowing does not replace mutability, especially when it's less costly to actually change properties of that variable; Rust has a specific design pattern for that!
推薦閱讀
- GitHub Essentials
- 計算機組成原理與接口技術:基于MIPS架構實驗教程(第2版)
- 在你身邊為你設計Ⅲ:騰訊服務設計思維與實戰
- 復雜性思考:復雜性科學和計算模型(原書第2版)
- Developing Mobile Games with Moai SDK
- 使用GitOps實現Kubernetes的持續部署:模式、流程及工具
- 企業大數據系統構建實戰:技術、架構、實施與應用
- 云計算服務保障體系
- Spark核心技術與高級應用
- Mastering Machine Learning with R(Second Edition)
- Hadoop大數據開發案例教程與項目實戰(在線實驗+在線自測)
- 數據分析師養成寶典
- Oracle 11g數據庫管理員指南
- Visual Studio 2012 and .NET 4.5 Expert Development Cookbook
- ORACLE 11g權威指南