- 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!
推薦閱讀
- 漫話大數(shù)據(jù)
- 數(shù)據(jù)挖掘原理與實(shí)踐
- 企業(yè)大數(shù)據(jù)系統(tǒng)構(gòu)建實(shí)戰(zhàn):技術(shù)、架構(gòu)、實(shí)施與應(yīng)用
- 文本挖掘:基于R語言的整潔工具
- Sybase數(shù)據(jù)庫在UNIX、Windows上的實(shí)施和管理
- 基于Apache CXF構(gòu)建SOA應(yīng)用
- 企業(yè)級容器云架構(gòu)開發(fā)指南
- 淘寶、天貓電商數(shù)據(jù)分析與挖掘?qū)崙?zhàn)(第2版)
- Oracle數(shù)據(jù)庫管理、開發(fā)與實(shí)踐
- 離線和實(shí)時(shí)大數(shù)據(jù)開發(fā)實(shí)戰(zhàn)
- 大數(shù)據(jù)技術(shù)體系詳解:原理、架構(gòu)與實(shí)踐
- MySQL性能調(diào)優(yōu)與架構(gòu)設(shè)計(jì)
- 掌中寶:電腦綜合應(yīng)用技巧
- AutoCAD基礎(chǔ)與應(yīng)用精品教程(2008版)
- Oracle數(shù)據(jù)庫性能優(yōu)化的藝術(shù)